[英]Why is my program not printing out after the input?
Ok so I'm making a rock paper scissors program in java and I have all the code and it should give me the output I want but every time i run it it only prints out the input statement and nothing else.好的,所以我正在用 java 制作一个石头剪刀布程序,我有所有的代码,它应该给我我想要的输出,但每次我运行它时,它只打印输入语句,没有别的。 I made it print a line after the do loop that the input statement is located and it prints that but it doesn't print anything that's in the if statements and I don't quite understand why.
我让它在输入语句所在的 do 循环之后打印一行并打印出来,但它不打印 if 语句中的任何内容,我不太明白为什么。 This is what I have.
这就是我所拥有的。 Thank you for any help provided.
感谢您提供的任何帮助。
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
String replay = ("");
String user = ("");
int ai = 0;
String aiPlay = ("");
do
{
ai = rand.nextInt(3) + 1;
switch (ai) //was if statements but java suggested to change to switch statements
{
case 1:
aiPlay = ("Rock");
break;
case 2:
aiPlay = ("Paper");
break;
default:
aiPlay = ("Scissors");
break;
}
do //make sure input is correct
{
System.out.print("rock, paper, or scissors: ");
user = input.next();
}while(!(user.equals("rock") || user.equals("paper") || user.equals("scissors")));
if(user.equals(ai))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nIt was a tie!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
switch (user) { //was if statements java suggested it change to switch statement
case "rock":
if (aiPlay.equals("scissors"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
else if (aiPlay.equals("paper"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
System.out.print("Continue? (y or n): ");
replay = input.next();
} break;
case "paper":
if (aiPlay.equals("scissors"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
else if (aiPlay.equals("rock"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
System.out.print("Continue? (y or n): ");
replay = input.next();
} break;
case "scissors":
if (aiPlay.equals("paper"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
System.out.print("Continue? (y or n): ");
replay = input.next();
}
else if (aiPlay.equals("rock"))
{
System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
System.out.print("Continue? (y or n): ");
replay = input.next();
} break;
default:
break;
}
}while(replay.equals("y"));
}
switch (ai) //was if statements but java suggested to change to switch
statements
{
case 1:
aiPlay = ("rock"); /* All should be lowercase*/
break;
case 2:
aiPlay = ("paper"); /* All should be lowercase*/
break;
default:
aiPlay = ("scissors"); /* All should be lowercase*/
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.