错误
在线程“
”java.util.NoSuchElementException中出现异常:在CircleFormulas.main(scanner.java:22)的主线程中找不到任何行
My code
import java.util.Scanner;
public class CircleFormulas {
public static void main(String[] args) {
//Creates my constructors/variables
//Scans for the user's name
Scanner userName1 = new Scanner (System.in);
//Scans for the radius the user wishes to calculate
Scanner user_Radius = new Scanner (System.in);
//Asks the user for their name and places their response into the userName variable
System.out.println("What is your name?: ");
String userName = userName1.nextLine();
//closes the line function; locks the variable
userName1.close();
//Prints out a greeting for the user
System.out.println("Hey " + userName + " How are you?");
//Asks the user a question
System.out.println("Now, what is the radius of the circle you'd like to calculate?");
//Asks the user for their radius they'd like to calculate and places their response into the radius variable
String radius = user_Radius.nextLine();
user_Radius.close();
//Prints out the radius of the user's circle
System.out.println("So, the radius of your circle is: " + radius);
}
}
发布于 2018-10-21 00:29:43
出现错误是因为您试图重用关闭的System.in
(即使它在另一个变量中声明,但仍然是关闭的)。
您不需要实例化多个扫描器,只需执行一次并多次重用就足够了:
Scanner scanner = new Scanner(System.in);
String userName = scanner.nextLine();
String radius = scanner.nextLine();
scanner.close();
https://stackoverflow.com/questions/52907697
复制相似问题