我正在创建一个摇滚,纸张,剪刀游戏来试用我的Java技能。在我的代码中,我有一个循环循环,直到胜利者赢或输了他们的数量。这部分不起作用。它直接进入逻辑语句,即使它没有完成该循环,它仍然在while循环之外。
代码:
package rps_game;
// Rock, Paper, Scissors game
//
import java.util.*;
public class rock_paper_scissors {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
Scanner lineInput = new Scanner(System.in);
Random rand = new Random();
String choices[] = { "rock", "paper", "scissors" };
int winRounds = 0;
boolean running = true;
int rounds = 0;
int wins = 0;
int loses = 0;
String yourChoice = "";
String compChoice = "";
while (running = true) { // main loop
try {
System.out
.println("Enter the amount of rounds you want to play: ");
rounds = input.nextInt(); // gets input
winRounds = (rounds / 2) + 1;
System.out.println("You are playing best of " + winRounds
+ " out of " + rounds);
//running = false; // breaks off from loop
while (wins < winRounds && loses < winRounds) { // this while loop
System.out.println("Enter either Rock, Paper or Scissors: ");
yourChoice = lineInput.nextLine();
yourChoice= yourChoice.toLowerCase();
if (Arrays.asList(choices).contains(yourChoice)) { // what use entered
break;
} else {
System.out.println("You did not enter either Rock, Paper or Scissors.");
System.exit(0);
}
compChoice = choices[rand.nextInt(choices.length)];
System.out.println("ROCK.. PAPER.. SCISSORS!");
System.out.println("You entered: " + yourChoice + " and the computer entered: " + compChoice);
if (yourChoice.equals(compChoice)){
System.out.println("\nIt's a draw!");
}else if(yourChoice.equals("rock") && compChoice.equals("paper")){
System.out.println("\nYou lose!");
loses++;
}else if(yourChoice.equals("paper") && compChoice.equals("rock")){
System.out.println("\nYou win!");
wins++;
}else if(yourChoice.equals("scissors") && compChoice.equals("paper")){
System.out.println("\nYou win!");
wins++;
}else if(yourChoice.equals("paper") && compChoice.equals("Scissors")){
System.out.println("\nYou lose!");
loses++;
}else if(yourChoice.equals("rock") && compChoice.equals("scissors")){
System.out.println("\nYou win!");
wins++;
}else if(yourChoice.equals("scissors") && compChoice.equals("rock")){
System.out.println("\nYou lose!");
loses++;
}else{
break;
}
}
System.out.println("\nGAME FINISHED!");
if (wins > loses){
System.out.println("\nYOU WIN THE GAME!!");
}else if(wins < loses){
System.out.println("\nYOU LOST THE GAME!! :( ");
}else{
System.out.println("It's a draw!!");
System.exit(0);
}
} catch (Exception e) { // if error pops up
System.out.println("You DID NOT enter a WHOLE NUMBER.");
break;
}
}
input.close();
lineInput.close();
}
}
为什么它不像它应该的那样循环?我真的无法解决这个问题
答案 0 :(得分:4)
问题是你的休息陈述
if (Arrays.asList(choices).contains(yourChoice)) { // what use entered
break;
}
break
会导致您退出循环,并且当您选择正确的输入时,我不认为您想要退出循环。
事实上,因为你在System.exit
块中也有else
命令,所以在这两行之后你的内部while循环都不会执行,无论如何。
if (Arrays.asList(choices).contains(yourChoice)) { // what use entered
break;
} else {
System.out.println("You did not enter either Rock, Paper or Scissors.");
System.exit(0);
}
我建议删除break
语句,并将System.exit(0);
更改为continue
,这将跳转到循环的下一次迭代。如果这样做,您也可以重新使用if-logic,因为只有1个条件。
答案 1 :(得分:0)
就像@David Ehrmann所说,你需要将running = true
更改为running == true
或仅while(running)
,这个if语句中的逻辑是错误的:
if (Arrays.asList(choices).contains(yourChoice)) { // what use entered
break;
} else {
System.out.println("You did not enter either Rock, Paper or Scissors.");
System.exit(0);
}
输入正确的输入后,您不希望break
。适当的if语句应该是:
if (!(Arrays.asList(choices).contains(yourChoice)))
{
System.out.println("You did not enter either Rock, Paper, or Scissors.");
running = false;
break;
}
else
{
// the rest of the playing code
}
这假定您要优雅地退出。它应该打印“这是一个平局。”并以这种方式清理任何资源......如果这就是你想要的。