public class Pig
{
public static void main(String[] args)
{
int turnScores = 0;
int totalScores = 0;
int turnScores2 = 0;
int totalScores2 = 0;
int dice;
int dice2;
String input = "r";
char repeat;
Scanner keyboard = new Scanner(System.in);
Random randomNumbers = new Random();
System.out.println("Welcome to the game of Pig!\n");
while(totalScores < 100 || totalScores2 < 100)
{
//human's turn
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScores = 0;
System.out.print("Your lose your turn!");
System.out.println("Your Total is " + totalScores);
break;
}
else
{
turnScores += dice;
System.out.print("Your turn score is " + turnScores);
System.out.println(" and your total scores is " + totalScores);
System.out.println("If you hold, you will have " + turnScores
+ " points.");
System.out.println("Enter 'r' to roll again, 'h' to hold.");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 'h')
{
break;
}
}
}while(input.equalsIgnoreCase("r") || dice != 1);
totalScores += turnScores;
System.out.println("Your scroes is " + totalScores);
if(totalScores >= 100)
{
System.out.println("Your total Scores is " + totalScores);
System.out.println("YOU WIN!");
break;
}
//computer's turn
System.out.println();
System.out.println("It is the compuer's turn.");
do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("The computer rolled: " + dice2);
if(dice2 == 1)
{
turnScores2 = 0;
System.out.print("The computer lost its turs!");
System.out.println(" Computer total is " + totalScores2);
break;
}
else
{
turnScores2 += dice2;
if(turnScores2 >= 20 || (totalScores2 + turnScores2) >= 100 )
{
System.out.println("The computer holds");
break;
}
}
}while(dice2 != 1 || turnScores2 < 20);
totalScores2 += turnScores2;
System.out.println("The computer's scores is " + totalScores2 + "\n");
if(totalScores2 >= 100);
{
System.out.println("THE COMPUTER WINS!");
break;
}
}
}
}
这些是我的陈述。我不知道为什么我的陈述不循环。 我的结果是:
Welcome to the game of Pig! You rolled: 4 Your turn score is 4 and your total scores is 0 If you hold, you will have 4 points. Enter 'r' to roll again, 'h' to hold. h Your score is 4 It is the computer's turn. The computer rolled: 6 The computer rolled: 4 The computer rolled: 2 The computer rolled: 5 The computer rolled: 5 The computer holds The computer's scores is 22 THE COMPUTER WINS!
答案 0 :(得分:1)
正如我之前所说,你必须从本声明的末尾删除半冒号:
if(totalScores2 >= 100);
如果不删除该语句,则将if语句视为已关闭的语句,跳过,执行print语句,然后执行break
。在那个阶段,你处于整个while
循环中,break将简单地退出系统
然而,即使修复也不会使游戏功能完全正确。这是因为您只在骰子滚动= 1时重置turnScores
。这是不对的。
如果我的得分为20且选择保持,那么需要将其重置为0,否则当我开始下一次开始时,我将有20分开始。
解决方案是在转弯结束时得分的打印声明之后重置turnScore,如下所示:
totalScores += turnScores;
System.out.println("Your scroes is " + totalScores);
turnScores = 0;
然后是电脑:
totalScores2 += turnScores2;
System.out.println("The computer's scores is " + totalScores2 + "\n");
turnScores2 = 0;
答案 1 :(得分:0)
我永远注意到if(totalScores2 >= 100);
有“;”到底。删除它或if语句没有意义