有一个 Backpack 系统使用 println 来 ++ 整数,但它不保存并且 Math.random() 每次都保持不变并且不重置。
我希望整个循环重新开始,但要保存背包所在的 While 循环。
your text
// 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");
}
如何不重置任何整数?
我假设你的“背包”由以下值组成:
int common = 0;
int rare = 0;
int epic = 0;
int legendary = 0;
int exotic = 0;
int unique = 0;
我在原来的评论中的意思是,如果您将
do-while
循环封装在另一个循环中,请不要再次将背包值设置为 0。例如:
...
while (some_condition) {
// don't reset backpack values
do {
HighLow = Math.random() * 101;
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);
}
这样,您的“背包”项目计数将在
do-while
执行之间“保存”。