游戏的人体部分似乎工作正常,但一旦我进入了#39;为了结束玩家,随着计算机的推出,代码进入无限循环。如果转弯由于1次滚动而不会循环。有人能指出我失踪的东西吗?提前谢谢!
import java.util.Random;
import java.util.Scanner;
public class Proj3Part2 {
public static void main(String[] args) {
int playerTurnScore = 0;
int playerTotScore = 0;
int compTotScore = 0;
int compTurnScore = 0;
int turn = 1;
int dice = 0;
int counter = 0;
Scanner input = new Scanner (System.in);
Random roll = new Random();
final int WIN = 20;
char decision = 'r';
boolean gameOver = false;
boolean human = true;
System.out.print("Your turn total is: " + playerTurnScore + ". Enter <r>oll or <s>top: ");
decision = input.next().charAt(0);
while (gameOver == false) {
while (turn == 1) {
if (decision == 'r' || decision == 'R') {
dice = (int)(Math.random()*6) + 1;
System.out.println("You rolled " + dice);
if (dice == 1) {
System.out.println("Turn over.");
System.out.println("\nCurrent score: You have " + playerTotScore + ", Computer has " + compTotScore);
playerTurnScore = 0;
turn = 2;
}
else {
playerTurnScore +=dice;
System.out.print("Your turn total is " + playerTurnScore + ". Enter <roll> or <s>top ");
decision = input.next().charAt(0);
}
}
else {
System.out.print("Turn over.");
System.out.println("\nCurrent score: You have " + (playerTotScore += playerTurnScore) + ", Computer has " + compTotScore);
playerTurnScore = 0;
turn = 2;
}
if (playerTotScore >= WIN) {
System.out.println("\nYou win!");
gameOver = true;
}
}
while (turn == 2) { //computer turn
System.out.print("Computer turn total is: " + compTurnScore);
System.out.println(" Computer rolls.");
dice = (int)(Math.random()*6) + 1;
System.out.println("Computer rolled: " + dice);
counter++;
if (dice == 1) {
System.out.print("Turn over.");
System.out.println("\nCurrent score: You have " + playerTotScore + ", Computer has " + compTotScore);
compTurnScore = 0;
turn = 1;
break;
}
if (counter < 3) {
compTurnScore += dice;
dice = (int)(Math.random()*6) + 1;
counter++;
}
if (counter >= 3) {
compTurnScore += dice;
System.out.print("Turn over.");
compTotScore += compTurnScore;
System.out.println("\nCurrent score: You have " + playerTotScore + ", Computer has " + compTotScore);
turn = 1;
break;
}
}
}
System.exit(0);
}
}
答案 0 :(得分:0)
单步执行调试器中的代码我可以看到你有
System.out.print("Your turn total is " + playerTurnScore + ". Enter <roll> or <s>top ");
decision = input.next().charAt(0);
在你的第一个循环中,所以它询问接下来要做什么,但是你在第二个循环中没有这个。