我有一个工作的10面骰子掷骰子HiLo猜谜游戏,正确为:
public static void main(String args[]) {
Scanner input = new Scanner (System.in);
//roll one
int rNum1 = (int)(10*Math.random()+1); //declare random roll one, gives result between 1 and 10
//print roll one
System.out.println("You roll the 10-sided die.");
System.out.println("It lands on " + rNum1 + "."); //result between 1 and 10
System.out.println(); //space
//User guess
System.out.println("Will a second die roll higher or lower?");
System.out.println("To guess higher enter [1], or to guess lower, enter [2].");
//roll two
int rNum2 = (int)(10*Math.random()+1); //declare random roll two, gives result between 1 and 10
int guess = input.nextInt(); //user's guess / HiLo
//invalid user entry
while(guess != 1 && guess != 2)
{
System.out.println("Invalid entry"); // print incorrect guess entry input
System.out.println("To guess higher enter [1], or to guess lower, enter [2].");
guess = input.nextInt(); //reset scanner "input"
}
//print roll two
System.out.println(); //space
System.out.println("You roll the die again.");
System.out.println("It lands on " + rNum2 + "."); //result between 1 and 10
System.out.println(); //space
if (guess == 1) //guessed higher
{
if (rNum1 == rNum2)
{
System.out.println("Draw!");
}
else if (rNum1 > rNum2)
{
System.out.println("You guessed incorrectly.");
}
else
{
System.out.println("You guessed correctly!");
}
}
if (guess == 2) //guessed lower
{
if (rNum1 == rNum2)
{
System.out.println("Draw!");
}
else if (rNum1 > rNum2)
{
System.out.println("You guessed correctly!");
}
else
{
System.out.println("You guessed incorrectly.");
}
}
现在我试图允许用户下注。允许用户根据正确的HiLo猜测从他们的底池(双钱)设置初始值(双初始)和工资(双赌注)。如果他们输了,他们就会失去赌注,如果他们赢了,他们就会获得150%的赌注:
if (guess == 2) //guessed lower
{
if (rNum1 == rNum2) //draw
{
System.out.println("Draw! Nothing lost, nothing gained!");
}
else if (rNum1 > rNum2) //correct guess
{
System.out.println("You guessed correctly!");
money = money*1.5; //150% gain
run++; //adds 1 to run
}
else //incorrect guess
{
System.out.println("You guessed incorrectly.");
money = money - wager; //wager lost
}
}
//total
total = (initial + wager) - money;
total = (initial + wager) - money;
我还将程序包含在while循环中(其中boolean cashOut为false),以便用户可以选择在每次滚动后通过现金结束完成游戏(将cashOut更改为true)或者如果pot达到零。
while(money != 0 && (cashOut = false)) {
}
我设置了printlns,在兑换或游戏结束后,返回正确猜测的数量以及下注和兑现的总金额。
System.out.println("Game over! Thanks for playing!");
System.out.println("Correct guesses = " + run + ".");
System.out.println("Total bet = " + total + ".");
System.out.println("Cashed out = " + money + ".");
然而,现在当向用户询问初始值(10到100之间)时,系统不返回任何内容,无论是否满足验证条件。我试图调试程序6个小时无济于事。任何人都可以发现我的错误吗?
===================
完整代码:
public static void main(String args[]) {
int run = 0; //correct guesses
Scanner input = new Scanner (System.in);
//user's stakes
System.out.println("How much cash are you betting with today? (Enter int 10 & 100)");
double initial = input.nextDouble();
//invalid user stake entry
while(!(initial < 10 && initial > 100));
{
System.out.println("Invalid entry"); // print incorrect guess entry input
System.out.println("Enter between 10 & 100.");
initial = input.nextInt(); //reset scanner "input"
}
boolean cashOut = false;
double money = initial;
double total = initial;
while(money != 0 && (cashOut = false))
{
System.out.println("What is your wager? (to 1 d.p.)");
double wager = input.nextDouble();
//roll one
int rNum1 = (int)(10*Math.random()+1); //declare random roll one, gives result between 1 and 10
//print roll one
System.out.println("You roll the 10-sided die.");
System.out.println("It lands on " + rNum1 + "."); //result between 1 and 10
System.out.println(); //space
//User guess
System.out.println("Will a second die roll higher or lower?");
System.out.println("To guess higher enter [1], or to guess lower, enter [2].");
//roll two
int rNum2 = (int)(10*Math.random()+1); //declare random roll two, gives result between 1 and 10
int guess = input.nextInt(); //user's guess / HiLo
//invalid user entry
while(guess != 1 && guess != 2)
{
System.out.println("Invalid entry"); // print incorrect guess entry input
System.out.println("To guess higher enter [1], or to guess lower, enter [2].");
guess = input.nextInt(); //reset scanner "input"
}
//print roll two
System.out.println(); //space
System.out.println("You roll the die again.");
System.out.println("It lands on " + rNum2 + "."); //result between 1 and 10
System.out.println(); //space
if (guess == 1) //guessed higher
{
if (rNum1 == rNum2)
{
System.out.println("Draw! Nothing lost, nothing gained!");
}
else if (rNum1 > rNum2)
{
System.out.println("You guessed incorrectly.");
money = money - wager; //wager lost
}
else
{
System.out.println("You guessed correctly!");
money = money*1.5; //150% gain
run++; //adds 1 to run
}
}
if (guess == 2) //guessed lower
{
if (rNum1 == rNum2)
{
System.out.println("Draw! Nothing lost, nothing gained!");
}
else if (rNum1 > rNum2)
{
System.out.println("You guessed correctly!");
money = money*1.5; //150% gain
run++; //adds 1 to run
}
else
{
System.out.println("You guessed incorrectly.");
money = money - wager; //wager lost
}
}
//total
total = (initial + wager) - money;
//cashout
System.out.println("To cash out enter [cp]");
String cashoutChoice = input.next().toLowerCase();
if (cashoutChoice.equals("cp"))
{
cashOut = true;
}
System.out.println(); //space
}
System.out.println("Game over! Thanks for playing!");
System.out.println("Correct guesses = " + run + ".");
System.out.println("Total bet = " + total + ".");
System.out.println("Cashed out = " + money + ".");
}
}
答案 0 :(得分:0)
在您的第一个 while 循环中,您已在循环条件结束时放置了分号(;)。删除它,因为这只是强迫事情认为这是而之后的空语句,然后迫使EDT无限期地在此行上循环,直到应用程序被手动强制为关闭。
您的情况也有误。由于您只需要包含10到100之间的值,因此您真正想要的条件是:
while (initial < 10 || initial > 100) { ... }
执行整个循环的更好方法可能是:
//user's stakes
double initial = 0.0;
//invalid user stake entry
while (initial < 10 || initial > 100) {
System.out.println("How much cash are you betting with today?\n"
+ "Enter a value between 10 & 100: ");
initial = input.nextDouble();
if (initial < 10 || initial > 100) {
// Print incorrect input entry
System.err.println("Invalid entry!\n");
input.nextLine(); // ** Clear Scanner Buffer.
}
}
如果提供的字母字符不是小数点,最终会抛出 InputMismatchException ,那么这并不能解决问题。 StackOverflow 中很多代码示例,向您展示如何处理此问题。搜索它们。
但是,这只是你问题的一部分。在提供初始赌注值后,您的代码现在将直接转到 游戏结束 ,因为下一个中提供的条件在您的代码中循环,下注提示循环:
boolean cashOut = false;
double money = initial;
double total = initial;
while (money != 0 && (cashOut = false)) { .... }
在 while 循环之上,您声明并将布尔 cashOut 变量初始化为 false ,这很好,但在而< / strong>循环条件你不断强迫提款等于错误,因为你在条件中使用了一个等于( = )的字符。 =
与==
不同,后者应在您的条件中使用:
while (money != 0 && cashOut == false) { .... }
在条件中使用单个等号只会使 cashOut 变量等于后面放置的布尔值(在您的情况下为 false )。在条件语句中使用==
。
这应该让你去。同时考虑不允许用户提供更大的赌注余额。