当前位置: 首页 > news >正文

积分兑换小程序Java

某个学校为了激励学生踊跃参加一些社会实践活动,会对参与者给予一些校园积分,学生们获得校园积分后可以使用校园积分在指定的老师那兑换一些学习用具,当前可兑换的物品和对应的积分数量如下:

铅笔1分

橡皮2分

作业本3分

文具盒5分

为了方便学生进行兑换,现在需要实现一个积分兑换小程序,输入所兑换的用具编号进行兑换,并且兑换后可以选择继续兑换,控制台输出可以继续兑换的用具个数和剩余积分。假设小明现有20分可以兑换些什么?

解法一:

package lianxi1;import java.util.Scanner;public class exchange {public static void main(String[] args) {int points = 20; // 初始积分Scanner scanner = new Scanner(System.in);System.out.println("---------------积分兑换小程序-------------------");// 打印铅笔所需积分System.out.println("1. 铅笔所需积分为:1分");// 打印橡皮所需积分System.out.println("2. 橡皮所需积分为:2分");// 打印作业本所需积分System.out.println("3. 作业本所需积分为:3分");// 打印文具盒所需积分System.out.println("4. 文具盒所需积分为:5分");System.out.println("您的初始积分为:" + points+"分");System.out.println("每次兑换后剩余积分会累计,您可以选择继续兑换或退出。");System.out.println("------------------------------------------------");while (true) {// 如果积分不足,提示并退出if (points < 1) {System.out.println("剩余积分不足,无法继续兑换。");break;}System.out.print("请输入需要兑换的物品编号(或输入0退出):");int itemId = scanner.nextInt();if (itemId == 0) {System.out.println("退出积分兑换小程序。");break;}// 兑换前检查积分是否足够switch (itemId) {case 1: // 铅笔if (points >= 1) {points -= 1;System.out.println("成功兑换铅笔,剩余积分:" + points+"分");int pencil = points / 1;int pencilPoints = points % 1;System.out.println("当前积分:" + points + "分,还可以兑换铅笔" + pencil + "个,剩余积分:" + pencilPoints+"分");} else {System.out.println("积分不足,无法兑换铅笔!");}break;case 2: // 橡皮if (points >= 2) {points -= 2;System.out.println("成功兑换橡皮,剩余积分:" + points+"分");int eraser = points / 2;int eraserPoints = points % 2;System.out.println("当前积分:" + points + "分,还可以兑换橡皮" + eraser + "个,剩余积分:" + eraserPoints+"分");} else {System.out.println("积分不足,无法兑换橡皮!");}break;case 3: // 作业本if (points >= 3) {points -= 3;System.out.println("成功兑换作业本,剩余积分:" + points+"分");// 输出当前积分和可兑换的作业本数量int notebooks = points / 3;int remainingPoints = points % 3;System.out.println("当前积分:" + points + "分,还可以兑换作业本" + notebooks + "个,剩余积分:" + remainingPoints+"分");} else {System.out.println("积分不足,无法兑换作业本!");}break;case 4: // 文具盒if (points >= 5) {points -= 5;System.out.println("成功兑换文具盒,剩余积分:" + points+"分");int writingcase= points / 5;int writingPoints = points % 5;System.out.println("当前积分:" + points + "分,还可以兑换文具盒" + writingcase + "个,剩余积分:" +writingPoints+"分");} else {System.out.println("积分不足,无法兑换文具盒!");}break;default:System.out.println("无效的物品编号!");}// 询问是否还继续兑换System.out.println("您还想继续兑换吗?(是输入1,否输入0):");int continueOption = scanner.nextInt();if (continueOption == 0) {System.out.println("感谢使用,再见!");break;} }scanner.close();}
}

解法二:

package lianxi1;import java.util.Scanner;public class exchange {public static void main(String[] args) {int points = 20; // 初始积分Scanner scanner = new Scanner(System.in);System.out.println("---------------积分兑换小程序-------------------");// 打印铅笔所需积分System.out.println("1. 铅笔所需积分为:1分");// 打印橡皮所需积分System.out.println("2. 橡皮所需积分为:2分");// 打印作业本所需积分System.out.println("3. 作业本所需积分为:3分");// 打印文具盒所需积分System.out.println("4. 文具盒所需积分为:5分");System.out.println("您的初始积分为:" + points+"分");System.out.println("每次兑换后剩余积分会累计,您可以选择继续兑换或退出。");System.out.println("------------------------------------------------");while (true) {// 如果积分不足,提示并退出if (points < 1) {System.out.println("积分不足,无法继续兑换。");break;}System.out.print("请输入需要兑换的物品编号(或输入0退出):");int itemId = scanner.nextInt();if (itemId == 0) {System.out.println("退出积分兑换小程序。");break;}switch (itemId) {case 1: // 铅笔if (points >= 1) {points -= 1;System.out.println("成功兑换铅笔,剩余积分:" + points);displayRemaining(points, 1);} else {System.out.println("积分不足,无法兑换铅笔!");}break;case 2: // 橡皮if (points >= 2) {points -= 2;System.out.println("成功兑换橡皮,剩余积分:" + points);displayRemaining(points, 2);} else {System.out.println("积分不足,无法兑换橡皮!");}break;case 3: // 作业本if (points >= 3) {points -= 3;System.out.println("成功兑换作业本,剩余积分:" + points);displayRemaining(points, 3);} else {System.out.println("积分不足,无法兑换作业本!");}break;case 4: // 文具盒if (points >= 5) {points -= 5;System.out.println("成功兑换文具盒,剩余积分:" + points);displayRemaining(points, 5);} else {System.out.println("积分不足,无法兑换文具盒!");}break;default:System.out.println("无效的物品编号!");}System.out.println("您还想继续兑换吗?(是输入1,否输入0):");int continueOption = scanner.nextInt();if (continueOption == 0) {System.out.println("感谢使用,再见!");break;}}scanner.close();}// 方法:显示还能兑换多少个对应商品和剩余积分private static void displayRemaining(int points, int cost) {int maxCount = points / cost;int remainingPoints = points % cost;System.out.println("还可以用剩余积分兑换" + maxCount + "个商品,还剩" + remainingPoints + "分积分。");}
}

http://www.xdnf.cn/news/1195903.html

相关文章:

  • 深入理解 Spring 中的 XmlBeanFactory 原理及实践
  • 数据结构第1问:什么是数据结构?
  • Java 大视界 -- Java 大数据机器学习模型在电商客户细分与精准营销活动策划中的应用(367)
  • 【牛客网C语言刷题合集】(四)
  • PostgreSQL并发控制
  • 机器学习鸢尾花案例
  • KingbaseES聚焦产品上线
  • docker与k8s的容器数据卷
  • 自由学习记录(74)
  • 多租户Kubernetes集群架构设计实践——隔离、安全与弹性扩缩容
  • MYSQL--再谈间隙锁和临键锁
  • RabbitMq 常用命令和REST API
  • 商品中心—1.B端建品和C端缓存
  • Python-初学openCV——图像预处理(四)——滤波器
  • 命令行和neovim的git操作软件-lazygit
  • sealos 方式安装k8s5节点集群
  • 自动标注软件X-AnyLabeling的使用教程
  • 基于动态增强的 LLM 置信度方法研究
  • C语言中:形参与实参的那些事
  • [SAP ABAP] ALV报表练习4
  • Matlab自学笔记六十五:解方程的数值解法(代码速成)
  • 文件IO——bmp图像处理
  • 磁悬浮轴承转子不平衡质量控制策略设计:原理、分析与智能实现
  • 基于java的在线教育平台管理系统、在线学习系统的设计与实现
  • 零基础学习性能测试第三章:jmeter性能组件应用(事件,并发,定时器)
  • 哈尔滨←→南昌的铁路要道
  • AWD的攻击和防御手段
  • idea中无法删除模块,只能remove?
  • 2025年7月26日训练日志
  • 最优估计准则与方法(4)最小二乘估计(LS)_学习笔记