我正在创建一个Rock,Paper,Scissors游戏来练习我的Java技能。但是现在,我正面临一个我无法解决的问题。当我要求用户输入某些内容时,他们无法输入内容,而是直接进入后面的if语句。
代码: 包rps_game;
// Rock, Paper, Scissors game
import java.util.Scanner;
import java.util.Random;
import java.util.*;
public class rock_paper_scissors {
public static void main(String args[]){
Scanner input = 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
}catch(Exception e){ // if error pops up
System.out.println("You DID NOT enter a WHOLE NUMBER.");
// still runs b/c criteria has not been met.
}
while (wins < winRounds && loses < winRounds){
// problem arises here
System.out.println("Enter either Rock, Paper or Scissors: ");
yourChoice = input.nextLine();
input.nextLine();
yourChoice.toLowerCase();
if (yourChoice.equals(Arrays.asList(choices).contains(yourChoice))){ // if array contains what use entered
break;
}else{
System.out.println("You did not enter either Rock, Paper or Scissors.");
running = false; // exit program
}
compChoice = choices[rand.nextInt(choices.length)];
System.out.println(compChoice);
}
}
}
}
我还没有完成它,但是发生了什么?
答案 0 :(得分:0)
您必须阅读第String temp = input.nextLine();
行,然后在本地创建另一个Scanner
以阅读int
。因此,用户按下的输入(\ n)将被读取为一行,否则按下的输入将被解释为input.nextLine();
虽然循环是无限循环,但您也可能想要纠正它。
答案 1 :(得分:0)
此代码可以纠正您的错误,但您仍需要做很多事情。
问题首先是输入扫描仪。 (您需要两个输入扫描仪)
其次,在yourChoice.toLowerCase()中,它必须是yourChouice = yourChoice.toLowerCase(); 因为字符串是不可变的。
最后在yourChoice.equals(Arrays.asList(choices).contains(yourChoice))。这实际上必须是(Arrays.asList(choices).contains(yourChoice)。
Try Catch位于错误的位置,因此您的整个代码都在运行,请参阅以下代码,没有问题。
import java.util.Scanner;
import java.util.Random;
import java.util.*;
public class sda {
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) {
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.");
running = false; // exit program
}
compChoice = choices[rand.nextInt(choices.length)];
System.out.println(compChoice);
}
} catch (Exception e) { // if error pops up
System.out.println("You DID NOT enter a WHOLE NUMBER.");
break;
}
}
input.close();
lineInput.close();
}
}