我有一项任务是创建一个滚动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
答案 0 :(得分:1)
你需要在整个游戏中跟踪两者。如果你有比以前更多的钱,请注意金额和它带你到达那里的数量。游戏结束后,您可以打印两个值。