我有一项任务要创建一个程序,该程序可以掷2个6面骰子。你每次掷骰子都要花费1美元。然而,当你掷7时,你会获得4美元。这个循环直到你没钱了。我已经记下了所有这些,但任务中有一个棘手的部分我无法理解。你也要追踪你在掷硬币的次数中拥有的最高金额,以及你拥有最高金额的是多少次。这看起来像的一个例子
当你有127美元的时候,你应该在43次掷完之后停止比赛。
我不知道如何追踪比赛中赚的最高金额。这是我的代码,谢谢你的帮助。
package dicegamee;
import java.util.Random;
import java.util.Scanner;
public class Dicegamee {
public static void main(String[] args) {
int diceone = 0; // declaring dice one as an int
int dicetwo = 0; // declaring dice two as an int
int money = 0; // declaring money as an int
int count = 0;
Random rand = new Random(); // setting random
Scanner reader = new Scanner(System.in); // setting scanner
System.out.println( "Hello, welcome to the dice game, one dollar is one game, how many are you willing to play? : "); // opening message
money = reader.nextInt(); // setting money too what you enter
while (money > 0) {
diceone = rand.nextInt(6); //rolling dice one
dicetwo = rand.nextInt(6); //rolling dice two
count++;
if (diceone + dicetwo == 7) { // if dice one and dice two equal seven statemen
money += 4;
}
}
money--; //subtracting one from money entered
}
System.out.println("It took " + count + " rolls too lose all your money"); // how many times the dice equaled seven printed
您需要在整个游戏中跟踪两者。如果你的钱比以前多,请注意金额和到达目的地所需的卷数。游戏结束后,您可以打印这两个值。