Java 新手 - 只是想掌握它。程序执行如下:
What's your age?23
23
What's your name?Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at king.getName(king.java:25)
at king.main(king.java:9)
它试图运行的代码如下:
import java.util.*;
public class king {
public static void main(String[] args){
System.out.println(getAge());
System.out.println(getName());
}
public static int getAge(){
System.out.print("What's your age?");
Scanner scanner = new Scanner(System.in);
String age = scanner.next();
scanner.close();
int numberAge = Integer.parseInt(age);
return numberAge;
}
public static String getName(){
System.out.print("What's your name?");
Scanner newScanner = new Scanner(System.in);
String name = newScanner.next();
newScanner.close();
return name;
}
}