0
我做錯了什麼?在我的代碼線程「主」java.util.NoSuchElementException異常?
的錯誤是:
異常在線程 「主」 java.util.NoSuchElementException
在java.util.Scanner.throwFor(Scanner.java:862)
在java.util.Scanner中的.next(Scanner.java:1485)
在java.util.Scanner.nextInt(Scanner.java:2117)
在java.util.Scanner.nextInt(Scanner.java:2076)
在MyClass.main( MyClass.java:14)
下面是代碼:
import java.util.Scanner;
import java.util.Random;
public class MyClass {
public static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
int person,ai,aiint;
Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("Let's Play rock paper scissors! press 1 for rock 2 for paper 3 for scissors ");
System.out.println();
aiint=rand.nextInt(3)+1;
System.out.println("Enter your play: ");
person=scan.nextInt();
System.out.println("Computer play is: " +aiint);
System.out.println("Your play is:" +person);
if (person==aiint) {
System.out.println("It's a tie!");
}
else if (person==1){
if(aiint==2){
System.out.println("Paper beats rock, you lose!");
}
else if(aiint==3){
System.out.println("Rock beats scissors, you win!");
}
}
else if (person==2){
if(aiint==1){
System.out.println("Paper beats rock, you win!");
}
else if (aiint==3){
System.out.println("Scissors beats paper, you lose!");
}
}
else if (person==3){
if(aiint==1){
System.out.println("Rock beats scissors, you lose!");
}
else if(aiint==2){
System.out.println("Scissors beats paper. you win!");
}
}
}
}
任何原因'實例化'新的掃描儀(System.in)'兩次? –
我自己試了一下代碼。基本上工作正常。 – Duke