我做了一個程序,要求3個整數輸出類型的三角形。一切運行和編譯成功,但是,它似乎在它要求用戶在看他們是否想再次循環它的一部分,在線編譯器輸出錯誤:掃描儀NoSuchElementException
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1347) at Assignment5.main(Assignment5.java:56)
import java.util.Scanner;
public class Assignment5 {
public static void main (String[]args)
{
for (int a = 0; a < Integer.MAX_VALUE; a++)
{
Scanner userInput = new Scanner(System.in);
Scanner answer = new Scanner(System.in);
int x,y,z;
System.out.println("Enter the sides of the triangle: ");
x = userInput.nextInt();
y = userInput.nextInt();
z = userInput.nextInt();
Tri isos = new Tri(x,y,z);
Tri equal = new Tri(x,y,z);
Tri scalene = new Tri(x,y,z);
// check the equilateral triangle
System.out.println(equal.toString() + " triangle:");
if (equal.is_isosceles())
System.out.println("\tIt is isosceles");
else
System.out.println("\tIt is not isosceles");
if (equal.is_equilateral())
System.out.println("\tIt is equilateral");
else
System.out.println("\tIt is not a equilateral");
if (equal.is_scalene())
System.out.println("\tIt is scalene");
else
System.out.println("\tIt is not scalene");
System.out.println("Would you like to enter values again? (y/n)");
String input = answer.next(); //Exception is thrown from here
if (input.equals("y"))
{
System.out.println("ok");
}
else if(!input.equals("y"))
{
System.out.println("Ok, bye.");
break;
}
}
}
}
什麼是56行?我也想知道爲什麼你需要2臺掃描儀。 –
line 56: String input = answer.next(); \t \t 如果(input.equals( 「Y」)) \t { \t \t System.out的。的println( 「OK」); \t} \t否則,如果 \t { \t \t的System.out.println( 「好吧,再見。」)(input.equals( 「Y」)!); \t \t break; \t} – Ryan
經過測試,它適用於我。 – Aneesh