好的,所以我正在制作一个计算机程序游戏,询问用户的名字和他们想要的中奖号码。然后,我启动了代码。
这是我的代码:
import java.util.Scanner; //For user input
public class Guts
{
public static void main(String[] args)
{
//Variable declarations
String userName = "";
int winningNumber;
boolean done = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type your name.");
userName = keyboard.next();
System.out.println("Welcome " + userName + ", to a game of Guts!");
while (!done){
System.out.println("Enter the winning number (20-50):");
winningNumber = keyboard.nextInt();
if (winningNumber >= 20 && winningNumber <=50)
{
System.out.println("Awesome! Great number!");
done = true;
}
else
{
System.out.println("This number is incorrect. Please enter a number between 20 and 50.");
}
}
}
boolean humanTurn = true;
boolean computerTurn = true;
int dice;
int humanTurnScore, computerTurnScore;
int humanTotalScore = 0;
int computerTotalScore = 0;
public void roll()
{
dice = (int)(Math.random()*6) + 1;
}
public int humanTurnScore()
{
{
humanTurnScore = dice + humanTurnScore;
System.out.println("You rolled:" + dice);
System.out.println("Your turn score is " + humanTurnScore + ".");
} return humanTurnScore;
}
public void humanTurnZero()
{
humanTurnScore = 0;
}
public int computerTurnScore()
{
{
computerTurnScore = dice + computerTurnScore;
System.out.println("Computer has scored: " + computerTurnScore + " in its turn.");
} return computerTurnScore;
}
public void computerTurnZero()
{
computerTurnScore = 0;
}
public Guts()
{
humanGame();
if(!humanTurn)
{
computerTurn();
}
}
public int humanGame()
{
System.out.println("To start the game please press 's'.");
Scanner key = new Scanner(System.in);
String start = key.nextLine();
if(!start.equalsIgnoreCase("s"))
{
System.out.println("Make sure you are pressing 's'.");
humanGame();
}
if(start.equalsIgnoreCase("s"))
{
System.out.println("You pressed 's'.");
System.out.println("Lets start.");
do{
roll();
if(dice == 1)
{
System.out.println("You got 1 and you lost your turn.");
System.out.println("Computer's total score is: " + computerTotalScore);
humanTurnZero();
computerTurn();
}
else if(dice != 1)
{
humanTotalScore += dice;
if(humanTotalScore >= 50)
{
System.out.println("You rolled: " + dice);
System.out.println("Your total score is: " + humanTotalScore);
System.out.println("Congratulations, you win!");
System.exit(0);
}
humanTurnScore();
System.out.println("Your total score is: " + humanTotalScore);
System.out.println("Computer's total score is: " + computerTotalScore);
System.out.println("You can hold or roll again.");
System.out.println("To roll again press 'r' or 'h' to hold.");
Scanner keyboard = new Scanner(System.in);
String choice = keyboard.nextLine();
if(choice.equalsIgnoreCase("R"))
{
System.out.println("Lets roll again.");
roll();
if(!choice.equalsIgnoreCase("R"))
{
System.out.println("You didn't press 'r'. To make sure the program is running correctly please press 'r' to roll or 'h' to hold.");
humanGame();
}
}
if(choice.equalsIgnoreCase("h"))
{
System.out.println("Your total score is: " + humanTotalScore);
humanTurnZero();
computerTurn();
}
}
}while(humanTurn);
}return dice;
}
public int computerTurn()
{
System.out.println("Now it's computer turn.");
do {
roll();
if(dice != 1)
{
computerTotalScore += dice;
if(computerTotalScore >=50)
{
System.out.println("Computer rolled: " + dice);
System.out.println("Computer's total score is: " + computerTotalScore);
System.out.println("Game Over! the computer wins");
System.exit(0);
}
System.out.println("Computer rolled: " + dice);
System.out.println("Computer's total score is: " + computerTotalScore);
System.out.println("Your total score is: " + humanTotalScore);
computerTurnScore();
roll();
}
if(dice == 1)
{
System.out.println("Computer rolled a 1, it's turn is over.");
computerTurnZero();
humanGame();
}
}while (computerTurn);
return dice;
}
}
我不明白为什么编译器在循环时不会通过中奖号码做任何事情。
如何修复代码,以便阅读整个内容而不仅仅是第一部分?
答案 0 :(得分:0)
根据您的代码,JVM作为第一步来到主方法并执行您在那里提到的内容。根据您在main方法中的代码,JVM不能从while循环中出来,直到您输入一个介于20和50之间的数字。如果您的输入与给定的凭据匹配,JVM将退出循环并结束程序之后JVM无事可做。 如果你需要执行程序的其他部分(正如你在这里提到的那样),那么你必须创建一个类的对象。然后JVM将转到构造函数并执行这些操作。
在while循环之后:
while (!done) {
System.out.println("Enter the winning number (20-50):");
winningNumber = keyboard.nextInt();
if (winningNumber >= 20 && winningNumber <= 50) {
System.out.println("Awesome! Great number!");
done = true;
} else {
System.out.println("This number is incorrect. Please enter a number between 20 and 50.");
}
}
Guts g = new Guts();