所以我对我的代码做了相当多的更改,现在它符合要求,但我得到了错误的总数,它总是认为玩家 2 获胜,甚至在它达到“20”之前。由于某种原因,直到玩家 2 滚动之后它才会读取玩家 1 的总得分,然后它不会计算玩家 2 的回合总数。当我之前进行更改时,一件事会开始工作,但另一件事会停止,所以我把它带回到编译后开始出现问题的地方。
import java.util.*;
public class Proj3Part1
{
public static void main(String[] args)
{
int turnScore = 0;
int totalScore = 0;
int turnScore2 = 0;
int totalScore2 = 0;
final int WIN = 20;
int dice = 0;
int dice2 = 0;
String input = "r";
String input2 = "r";
char repeat;
Scanner keyboard = new Scanner(System.in);
Scanner s = new Scanner (System.in);
Random randomNumbers = new Random();
while(totalScore < WIN && totalScore2 < WIN)
{
//Player 1's turn
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScore = 0;
System.out.println("Turn over.");
System.out.println("Player 1 total is " + totalScore);
break;
}
else
{
turnScore += dice;
System.out.print("Player 1 turn total is " + turnScore + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over.");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input.equalsIgnoreCase("r") || dice != 1);
{
totalScore += turnScore;
}
if(totalScore >= WIN)
{
System.out.println("Your total Score is " + totalScore);
System.out.println("Player 1 wins!");
}
//player2's turn
System.out.println();
System.out.println("It is Player 2's turn.");
{ do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("Player 2 rolled: " + dice2);
if(dice2 == 1)
{
turnScore2 = 0;
System.out.print("Turn over");
System.out.println("Player 2 total is " + totalScore2);
break;
}
else
{
turnScore2 += dice2;
System.out.print("Player 2 total is " +turnScore2 + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input2.equalsIgnoreCase("r") && dice != 1); {
totalScore2 += turnScore2;
}
}
if(totalScore2 >= WIN);
{
System.out.println("Player 2 score is " + totalScore2 + "\n");
System.out.println("Player 2 wins");
break;
}
}
}
}
最佳答案
玩家2/计算机的回合循环中出现错误。它位于第一个 if-else 循环中,位于 else 部分。
输入keyboard.nextLine();
应该是
input = Keyboard.nextLine();
纠正错误后,它工作正常。
此外,请密切注意编译错误,它们会向您指出哪些行生成了所述错误。
修改:
我认为这符合您的预期。
import java.util.*;
public class Proj3Part1
{
public static void main(String[] args)
{
int turnScore = 0;
int totalScore = 0;
int turnScore2 = 0;
int totalScore2 = 0;
final int WIN = 20;
int dice = 0;
int dice2 = 0;
String input = "r";
String input2 = "r";
char repeat;
Scanner keyboard = new Scanner(System.in);
Scanner s = new Scanner (System.in);
Random randomNumbers = new Random();
while(totalScore < WIN && totalScore2 < WIN)
{
//Player 1's turn
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScore = 0;
System.out.println("Turn over.");
System.out.println("Player 1 total is " + totalScore);
break;
}
else
{
turnScore = dice; //removed +=??? think it's only the value of dice roll?
//either way it's used to compute total, which would be redundant if not
totalScore +=turnScore; //added to compute totalScore before turn is over
System.out.print("Player 1 turn total is " + totalScore + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over.");
System.out.print("Current score: Player 1 has " + totalScore); //previously total wasn't computed
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}while(input.equalsIgnoreCase("r"));
//totalScore += turnScore; was removed + curly braces that seemed to attach it to the above while loop
//it isn't needed due to totalScore now being calculated after dice is rolled when !=1(else portion)
if(totalScore >= WIN)
{
System.out.println("Your total Score is " + totalScore);
System.out.println("Player 1 wins!");
break; //added to break the loop if player 1 wins
}
//player2's turn
System.out.println();
System.out.println("It is Player 2's turn.");
do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("Player 2 rolled: " + dice2);
if(dice2 == 1)
{
turnScore2 = 0;
System.out.print("Turn over");
System.out.println("Player 2 total is " + totalScore2);
break;
}
else
{
turnScore2 = dice2; //removed += ... same as for player 1's turn
totalScore2 += turnScore2; //added totalScore2 calculations.
System.out.print("Player 2 total is " +totalScore2 + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input2.equalsIgnoreCase("r")); //{ <- incorrect brace + fixed loop for dice2 !=1, then removed it :P since you already did a check inside the do-while loop
//totalScore2 += turnScore2; removed, no longer is needed
//}
//} <- not needed nor is the brace that was infront of the do while loop.
if(totalScore2 >= WIN) //removed semicolon since it ended the if statement before it's body
{
System.out.println("Player 2 score is " + totalScore2 + "\n");
System.out.println("Player 2 wins");
break;
}
if(totalScore>totalScore2) //added loops to check which score is higher and terminate
{
System.out.println("Player 1 Wins!");
break;
}else if(totalScore==totalScore2){
System.out.println("It's a Tie!");
break;
}else if(totalScore<totalScore2){
System.out.println("Player 2 Wins!");
break;
}
}
}
}
我还建议安装一个 IDE,例如 Netbeans或Eclipse 。 IDE 将使您的生活变得更加轻松,尤其是在处理格式和语法错误时。
关于java - 小 pig 游戏-java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939919/