1
我有一个任务来创建一个程序,可以掷出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