Java武器抽取循环重置需求:保留背包计数与更新随机值
武器抽取与背包系统问题修复
问题概述
开发了武器抽取系统和背包系统,当前存在两个问题:
- 数值递增的计数无法在多次抽取间保存
Math.random()生成的数值固定不重置
需求:实现武器抽取循环可重复触发,同时保留背包的计数状态。
原代码
// weapon draw system double HighLow = (Math.random() * 101); String Common = "You found a Common Sword!"; String Rare = "You found a Rare Sword!"; String Epic = "You found a Epic Sword!"; String Legendary = "You found a Legendary Sword!"; String Exotic = "You found a Exotic Sword!"; String Unique = "You found a Unique Sword!"; int common = 0; int rare = 0; int epic = 0; int legendary = 0; int exotic = 0; int unique = 0; //picking the weapon with random numbers //i want this part to reset and pick another weapon while saving the backpack part do { System.out.println(HighLow); if (HighLow <= 60) { System.out.println(Common); common++; break; } else if (HighLow <= 80) { System.out.println(Rare); rare++; break; } else if (HighLow <= 90) { System.out.println(Epic); epic++; break; } else if (HighLow <= 95) { System.out.println(Legendary); legendary++; break; } else if (HighLow <= 99) { System.out.println(Exotic); exotic++; break; } else if (HighLow >= 99.9) { System.out.println(Unique); unique++; break; } //the backpack system i want it to save } while (common < 20); { System.out.println("Backpack"); System.out.println(common + " Common Weapons"); System.out.println(rare + " Rare Weapons"); System.out.println(epic + " Epic Weapons"); System.out.println(legendary + " Legendary Weapons"); System.out.println(exotic + " Exotic Weapons"); System.out.println(unique + " Unique Weapons"); }
问题根源
- 随机数固定:
HighLow的随机值仅在循环外生成一次,循环内未重新生成,导致每次抽取结果完全相同。 - 计数无法持久:原循环内每次判断后用
break直接跳出,循环仅执行一次;背包输出在循环结束后才执行,无法实现多次抽取后的计数保留。
修复后的代码
import java.util.Scanner; public class WeaponDrawSystem { public static void main(String[] args) { // 定义武器名称常量 final String COMMON = "你获得了一把普通长剑!"; final String RARE = "你获得了一把稀有长剑!"; final String EPIC = "你获得了一把史诗长剑!"; final String LEGENDARY = "你获得了一把传说长剑!"; final String EXOTIC = "你获得了一把异域长剑!"; final String UNIQUE = "你获得了一把独一无二的长剑!"; // 背包计数变量,放在循环外保证状态持久 int commonCount = 0; int rareCount = 0; int epicCount = 0; int legendaryCount = 0; int exoticCount = 0; int uniqueCount = 0; Scanner scanner = new Scanner(System.in); boolean keepDrawing = true; while (keepDrawing) { // 每次循环生成新的随机数(0-100之间) double randomValue = Math.random() * 101; // 判断抽取的武器品质 if (randomValue <= 60) { System.out.println(COMMON); commonCount++; } else if (randomValue <= 80) { System.out.println(RARE); rareCount++; } else if (randomValue <= 90) { System.out.println(EPIC); epicCount++; } else if (randomValue <= 95) { System.out.println(LEGENDARY); legendaryCount++; } else if (randomValue <= 99) { System.out.println(EXOTIC); exoticCount++; } else if (randomValue >= 99.9) { System.out.println(UNIQUE); uniqueCount++; } // 显示当前背包状态 System.out.println("\n当前背包:"); System.out.println(commonCount + " 把普通武器"); System.out.println(rareCount + " 把稀有武器"); System.out.println(epicCount + " 把史诗武器"); System.out.println(legendaryCount + " 把传说武器"); System.out.println(exoticCount + " 把异域武器"); System.out.println(uniqueCount + " 把独一无二的武器"); // 询问是否继续抽取 System.out.print("\n是否继续抽取?(y/n):"); String input = scanner.nextLine().trim().toLowerCase(); if (!input.equals("y")) { keepDrawing = false; System.out.println("抽取结束,感谢使用!"); } } scanner.close(); } }
修复说明
- 随机数重置:将随机数生成代码移到循环内部,每次抽取都会生成新的随机值,解决数值固定问题。
- 背包状态持久:计数变量定义在循环外部,每次抽取后更新计数,确保背包数据不会丢失。
- 循环逻辑优化:移除原代码中的
break,改用外部循环控制抽取流程,通过用户输入决定是否继续抽取,实现循环重启需求。 - 代码规范优化:用常量定义武器名称,变量名语义化,提升代码可读性。
内容的提问来源于stack exchange,提问作者Gaming_Cruizer
相关产品推荐
相关产品推荐

