[英]Java - Nested While Loop
Greetings Stack Overflow users, I come to you this evening for assistance on a Java program that I have created. Greetings Stack Overflow用户,今晚我来找你,帮助我创建一个Java程序。 I'm relatively new to Java so please excuse my ignorance on the topic.
我对Java比较新,所以请原谅我对这个主题的无知。 I have made a Java program that's a "Rock" "Paper" "Scissors" game and there seems to be an error in one of the statements.
我制作了一个Java程序,它是一个“Rock”“Paper”“Scissors”游戏,其中一个语句中似乎有错误。
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;
}
}
}
}
}
The problem that I'm facing is related to the winning calculations. 我面临的问题与中奖计算有关。 When I run the program and I enter rock repeatedly until I win, the output will be You win, "ROCK" beats " " but with any other option I get You win, "ROCK" beats "PAPER"
当我运行程序并且我反复进入摇滚直到我赢了,输出将是你赢了,“摇滚”击败“”但是除了任何其他选项我得到你赢了,“摇滚”击败“PAPER”
My question is, why am I getting an empty callback when playing rock? 我的问题是,为什么我在玩摇滚时会得到一个空的回调?
* Also if you'd be willing to point out any other suggestions to help out a novice that would be great. * 此外,如果你愿意指出任何其他建议,以帮助一个伟大的新手。 *
*
Math.random() * 3
is a number at least 0 and less than 3. Math.random() * 3
是一个至少为0且小于3的数字。
After casting it to an int, it is 0, 1, or 2. 将它转换为int后,它为0,1或2。
if(computerResult == 0) {
playerComputer = "ROCK";
}
else if(computerResult == 1) {
playerComputer = "PAPER";
}
else if (computerResult == 2) {
playerComputer = "SCISSORS";
}
Suggestions: 建议:
Be concise. 简明扼要。 You could change
你可以改变
String startGame;
startGame = input.nextLine();
startGame = startGame.toUpperCase();
to 至
String startGame = input.nextLine().toUpperCase();
It's more readable when you don't have to scroll and scroll. 当您不必滚动和滚动时,它更具可读性。
Also, know that equalsIgnoreCase()
exists. 另外,知道
equalsIgnoreCase()
存在。
This is not for a complete novice, but I would model the game using this code: 这不是一个完整的新手,但我会使用以下代码为游戏建模:
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 (…) {
…
}
}
That way you have some part of the code that handles the game itself, while some other code handles the user interaction. 这样,您可以使用一些代码来处理游戏本身,而其他一些代码则处理用户交互。
You will find that (int)(Math.random() * 3) results in 0 sometimes and never 3, which is what is giving you your blank, as you don't have a result for 0. 您会发现(int)(Math.random()* 3)有时会导致0而从不会导致3,这就是给你空白的结果,因为你没有0的结果。
Specifically, its when Math.random() returns less than .33 具体来说,它在Math.random()返回小于.33时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.