好的,所以我编写了一个简单的石头剪刀游戏,使用用户输入进行游戏。用户键入他们的选择,随机函数选择计算机,然后生成结果。在此之后程序终止,因为它已经完成。我想使用while循环,这样当游戏结束时,它会再次启动,然后如果用户键入退出或退出,程序将停止,这可以通过简单地说playerGo != exit
之类的东西轻松完成,玩游戏等等。但是,我不能让这个工作,请有人帮助我,我是一个Java菜鸟:)
import java.util.Scanner;
public class RockPaperScissors{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int compGoInt;
String compGo;
String playerGo;
System.out.println("You can type 'Exit' to quit the game at any time.");
System.out.print("Please enter your choice. Rock, Paper or Scissors: ");
playerGo = input.nextLine();
compGoInt = (int) (Math.random() * 3);
switch (compGoInt){
case 0:
compGo = "Rock";
break;
case 1:
compGo = "Paper";
break;
case 2:
compGo = "Scissors";
break;
default:
compGo = "Error";
System.out.println("Error.");
break;
}
if (playerGo.equals(compGo)){
System.out.println("Computer chooses "+compGo);
System.out.println("It's a draw!");
}
else if ((playerGo.equalsIgnoreCase("Rock") && compGo.equalsIgnoreCase("Scissors")) ||
(playerGo.equalsIgnoreCase("Paper") && compGo.equalsIgnoreCase("Rock")) ||
(playerGo.equalsIgnoreCase("Scissors") && compGo.equalsIgnoreCase("Paper"))){
System.out.println("Computer chooses "+compGo);
System.out.println("Player Wins!");
}
else if ((compGo.equalsIgnoreCase("Rock") && playerGo.equalsIgnoreCase("Scissors")) ||
(compGo.equalsIgnoreCase("Paper") && playerGo.equalsIgnoreCase("Rock")) ||
(compGo.equalsIgnoreCase("Scissors") && playerGo.equalsIgnoreCase("Paper"))){
System.out.println("Computer chooses "+compGo);
System.out.println("Computer Wins!");
}
else{
System.out.println("Something has gone wrong!");
System.out.println("Player chose "+playerGo);
System.out.println("Computer chose "+compGo);
}
}
}
答案 0 :(得分:0)
简单地说:
while(true) {
if(playerGo.equalsIgnoreCase("Exit")) break;
else //GameLogic
}
但是,如果我这样说,你应该让用户选择一个数字,因为任何字母输入都容易出错。
为澄清目的:
import java.util.Scanner;
public class RockPaperScissors{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int compGoInt;
String compGo;
String playerGo;
while(true) {
System.out.println("You can type 'Exit' to quit the game at any time.");
System.out.print("Please enter your choice. Rock, Paper or Scissors: ");
playerGo = input.nextLine();
if(playerGo.equalsIgnoreCase("Exit")) break; //Checks for exit condition.
else { //GameLogic
compGoInt = (int) (Math.random() * 3);
switch (compGoInt){
case 0:
compGo = "Rock";
break;
case 1:
compGo = "Paper";
break;
case 2:
compGo = "Scissors";
break;
default:
compGo = "Error";
System.out.println("Error.");
break;
}
if (playerGo.equals(compGo)){
System.out.println("Computer chooses "+compGo);
System.out.println("It's a draw!");
}
else if ((playerGo.equalsIgnoreCase("Rock") && compGo.equalsIgnoreCase("Scissors")) ||
(playerGo.equalsIgnoreCase("Paper") && compGo.equalsIgnoreCase("Rock")) ||
(playerGo.equalsIgnoreCase("Scissors") && compGo.equalsIgnoreCase("Paper"))){
System.out.println("Computer chooses "+compGo);
System.out.println("Player Wins!");
}
else if ((compGo.equalsIgnoreCase("Rock") && playerGo.equalsIgnoreCase("Scissors")) ||
(compGo.equalsIgnoreCase("Paper") && playerGo.equalsIgnoreCase("Rock")) ||
(compGo.equalsIgnoreCase("Scissors") && playerGo.equalsIgnoreCase("Paper"))){
System.out.println("Computer chooses "+compGo);
System.out.println("Computer Wins!");
}
else{
System.out.println("Something has gone wrong!");
System.out.println("Player chose "+playerGo);
System.out.println("Computer chose "+compGo);
}
}
}
}
}
答案 1 :(得分:0)
您可以添加布尔变量IsGameRunning = true。 将整个登录包装在一个while循环中,该循环检查IsGameRunning的状态。 将一个大小写添加到检查用户输入“-1”的switch语句中,当用户输入“-1”作为输入时,大小写将IsGameRunning变量更改为false。
简单而且应该工作
答案 2 :(得分:0)
boolean running = true;
do{
System.out.println("You can type 'Exit' to quit the game at any time.");
System.out.print("Please enter your choice. Rock, Paper or Scissors: ");
playerGo = input.nextLine();
running = !(playerGo.equalsIgnoreCase('Exit') || playerGo.equalsIgnoreCase('Quit'));
if(running){
//logic
compGoInt = (int) (Math.random() * 3);
...
}
}while(running)
同时 - >输入需要至少询问一次。
和
if (playerGo.equals(compGo)){
System.out.println("Computer chooses "+compGo);
System.out.println("It's a draw!");
}
playerGo.equals(compGo)
- > playerGo.equalsIgnoreCase(compGo)
与代码的其余部分一样