Java嵌套While循环
向Stack Overflow用户问好,今晚我来向你们寻求关于我创建的Java程序的帮助。我对Java比较陌生,所以请原谅我对这个话题的无知。我制作了一个Java程序,它是一个“石头”“布”“剪刀”游戏,其中一个语句似乎有错误Java嵌套While循环,java,while-loop,switch-statement,Java,While Loop,Switch Statement,向Stack Overflow用户问好,今晚我来向你们寻求关于我创建的Java程序的帮助。我对Java比较陌生,所以请原谅我对这个话题的无知。我制作了一个Java程序,它是一个“石头”“布”“剪刀”游戏,其中一个语句似乎有错误 import java.util.Scanner; public class TheAntlers { public static void main(String[] args) { int playerHumanWins = 0; int play
import java.util.Scanner;
public class TheAntlers {
public static void main(String[] args) {
int playerHumanWins = 0;
int playerComputerWins = 0;
int numberOfTies = 0;
int computerResult;
Scanner input = new Scanner(System.in);
while(true) {
String startGame;
String playerHuman;
String playerComputer = " ";
System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
startGame = input.nextLine();
startGame = startGame.toUpperCase();
if(startGame.equals("N")) {
System.out.println("NO!");
break;
}
else if(! startGame.equals("Y")) {
startGame = startGame.toLowerCase();
System.out.println("Sorry, " + startGame + " is not a valid entry...");
}
while(startGame.equals("Y")) {
System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": ");
playerHuman = input.nextLine();
computerResult = (int)(Math.random() * 3);
playerHuman = playerHuman.toUpperCase();
if(computerResult == 1) {
playerComputer = "ROCK";
}
else if(computerResult == 2) {
playerComputer = "PAPER";
}
else if (computerResult == 3) {
playerComputer = "SCISSORS";
}
switch (playerHuman) {
case "ROCK" :
if(playerComputer.equals(playerHuman)) {
System.out.println("Tie you both picked \"ROCK\"");
numberOfTies++;
}
else if(playerComputer.equals("PAPER")) {
System.out.println("Computer wins!");
playerComputerWins++;
}
else {
System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\"");
playerHumanWins++;
return;
}
break;
case "PAPER" :
if(playerComputer.equals(playerHuman)) {
System.out.println("Tie you both picked \"PAPER\"");
numberOfTies++;
}
else if(playerComputer.equals("ROCK")) {
System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\"");
playerHumanWins++;
return;
}
else {
System.out.println("Sorry, the computer won!");
playerComputerWins++;
}
break;
case "SCISSORS" :
if(playerComputer.equals(playerHuman)) {
System.out.println("Tie you both picked \"SCISSORS\"");
numberOfTies++;
}
else if(playerComputer.equals("PAPER")) {
System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\"");
playerHumanWins++;
return;
}
else {
System.out.println("Sorry, the computer won!");
playerComputerWins++;
}
break;
default:
playerHuman = playerHuman.toLowerCase();
System.out.println("Sorry, " + playerHuman + " is not a valid entry...");
break;
}
}
}
}
}
我面临的问题与获胜的计算有关。当我运行程序并反复输入rock直到我赢时,输出将是你赢,“rock”beats”“,但如果使用任何其他选项,我会得到你赢,“rock”beats“PAPER”
我的问题是,为什么我玩摇滚乐时会收到一个空的回调
*此外,如果你愿意指出任何其他建议,以帮助新手,这将是伟大的* Math.random()*3
是一个至少为0且小于3的数字
将其强制转换为int后,它是0、1或2
if(computerResult == 0) {
playerComputer = "ROCK";
}
else if(computerResult == 1) {
playerComputer = "PAPER";
}
else if (computerResult == 2) {
playerComputer = "SCISSORS";
}
建议:
简明扼要。你可以改变
String startGame;
startGame = input.nextLine();
startGame = startGame.toUpperCase();
到
当你不必不断滚动的时候,它更容易阅读
另外,要知道,equalsIgnoreCase()
是存在的。你会发现(int)(Math.random()*3)有时会导致0,但永远不会导致3,这就是你的空白,因为你没有0的结果
具体来说,当Math.random()返回的值小于.33时,这不是为一个完全的新手准备的,但我将使用以下代码对游戏进行建模:
enum Choice { ROCK, PAPER, SCISSORS }
enum Result { COMPUTER_WINS, TIE, HUMAN_WINS }
Result decide(Choice computer, Choice human) {
if (human == computer) {
return Result.TIE;
} else if (…) {
…
}
}
这样,您就有了处理游戏本身的部分代码,而其他一些代码处理用户交互。如果(computerResult==0){playerComputer=“ROCK”}或者如果(computerResult==1){playerComputer=“PAPER”}或者{playerComputer=“SCISSORS”}别忘了加上这个,如果你想要一个介于1和3之间的数字,你可以做:1+(int)Math.random()*3;很好@AmirAfghani,不过在这种情况下,0、1和2对于大多数程序员来说都是很自然的。你介意我再问你一个简单的问题吗?例如,如果玩家赢了,有没有办法输入if语句?我想在最后提供统计数据,并考虑添加类似if(playerHumanWins=1)的内容{我不认为我完全同意那句话。
enum Choice { ROCK, PAPER, SCISSORS }
enum Result { COMPUTER_WINS, TIE, HUMAN_WINS }
Result decide(Choice computer, Choice human) {
if (human == computer) {
return Result.TIE;
} else if (…) {
…
}
}