当前位置: 首页 > 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.dtcms.com/a/300531.html

相关文章:

  • Megatron 中的 TensorParallel, PipelineParallel, ContextParallel,ExpertParallel
  • PHP框架之Laravel框架教程:3. 数据库操作(简要)
  • PowerDesigner 画ER图并生成sql 教程
  • 【学习笔记】MimicGen: 基于人类演示的可扩展机器人学习数据生成系统
  • GIt学习——分布式版本控制工具
  • STL——list
  • 金融科技中的虚拟助手
  • 15.7 DeepSpeed实战:单卡38GB到多卡12GB,3倍效率提升的ZeRO-3配置全解
  • 【专题十五】BFS 解决 FloodFill
  • 多智能体系统设计:协作、竞争与涌现行为
  • 2025年7月25日-7月26日 · AI 今日头条
  • 【第六节】方法与事件处理器
  • 【计算机网络架构】网状型架构简介
  • C++ 多线程(一)
  • 详解力扣高频SQL50题之610. 判断三角形【简单】
  • Vscode的常用快捷键(摆脱鼠标计划)
  • [N1盒子] 斐讯盒子N1 T1通用刷机包(可救砖)
  • 金字塔降低采样
  • C语言:顺序表(上)
  • K8S 九 安全认证 TLS
  • 关于西门子博图基本指令的应用区别
  • VScode 支持 QNX 源码跳转
  • 【Python系列】从内存分析到性能剖析
  • Mysql 二进制安装常见问题
  • 2025年Solar应急响应公益月赛-7月wp
  • mac neo4j install verifcation
  • 论文阅读-IGEV
  • SecureCRT连接密钥交换失败
  • 基于LNMP架构的分布式个人博客搭建
  • 总结和对比Unity中的三种主要抗锯齿技术:FXAA、SMAA和TAA