我對Java很新,我試圖做一個基本的計算器。還有另一個問題,但我改變了固定它的東西,但它仍然不起作用。這裏是我的代碼:Java簡單的計算器錯誤
import java.util.Scanner;
class HelloWorld{
public static void main(String args[])
int num1;
int num2;
String op;
Scanner input = new Scanner(System.in);
System.out.println("Enter your first number");
num1 = input.nextInt();
System.out.println("Enter your second number");
num2 = input.nextInt();
System.out.println("Enter the operation");
op = input.nextLine();
if (op.equals("*")){
System.out.println("The answer is: " + (num2 * num1));
}
if (op.equals("/")){
System.out.println("The answer is: " + (num2/num1));
}
if (op.equals("+")){
System.out.println("The answer is: " + (num2 + num1));
}
if (op.equals("-")){
System.out.println("The answer is: " + (num2 - num1));
}
}
}
錯誤說:
Enter your first number
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at HelloWorld.main(HelloWorld.java:12)
我似乎無法找到什麼是錯(我有其簡單的東西,我錯過了)
爲什麼你需要第二個'Scanner'?哪一行是'12'? –
此錯誤適用於'HelloWorld'應用程序的'main()'方法。 –
我不認爲我這樣做。我擺脫了它,但它給了我同樣的錯誤。 – minipanda1