我是Java的新手。从Java开始练习它的完整参考。 现在发生异常,我已经完成了此链接中的所有建议 https://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner#=
但我仍然得到错误。我正在使用Eclipse IDE
代码在这里。
import java.util.Scanner;
public class Calculater {
/**
* @param args
*/
public static void main(String[] args) {
Scanner IO=new Scanner(System.in);
char input;
System.out.println("Please Enter \n '+' for Addition \n'-' for Subtraction \n '*' for Multiplication \n '/' for Division = ");
input=IO.next().charAt(0);
if(input=='+' || input=='-' || input=='*' || input=='/') {
IO.close();
} else {
System.out.println("You entered Wrong operater!!!!!!!!");
Calculater.main(args);
}
if(input == '+') {
Calculater cal=new Calculater();
cal.addition();
}
}
public void addition(){
Scanner IO= new Scanner(System.in);
double num1,num2,num3;
char choice;
System.out.println("Please Enter 1st Number = ");
if(IO.hasNextDouble()) {
num1=IO.nextDouble();
} else {
num1=0;
}
System.out.println("Please Enter 2nd Number = ");
if(IO.hasNextDouble()) {
num2=IO.nextDouble();
} else {
num2=0;
}
num3=num1+num2;
System.out.println("After Addtion Answer is = "+num3);
System.out.println("Do You Want more Calculation Enter 'Y' for Yes and any key for No = ");
choice=IO.next().charAt(0);
IO.close();
if (choice == 'Y') {
String[] args = {};
main(args);
} else {
System.exit(0);
}
}
}
OutPut是这个
Please Enter
'+' for Addition
'-' for Subtraction
'*' for Multiplication
'/' for Division =
+
Please Enter 1st Number =
Please Enter 2nd Number =
After Addtion Answer is = 0.0
Do You Want more Calculation Enter 'Y' for Yes and any key for No =
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Calculater.addition(Calculater.java:87)
at Calculater.main(Calculater.java:37)
答案 0 :(得分:5)
(从评论中移出)
问题似乎是关闭Scanner
然后尝试阅读它。只需删除那些天真的IO.close
来电,然后将其关闭,直至程序结束。
答案 1 :(得分:2)
我可以收集到的是它是在输入之前尝试读取加/减的数字,并且在继续申请之前不再等待输入。
例如,您正在使用.close();
方法关闭输入流,然后才能读取其他内容,因此请尝试删除这些代码行,直到应用程序结束。
申请表的输出:
Please Enter
'+' for Addition
+
Please Enter 1st Number =
Please Enter 2nd Number =
After Addtion Answer is = 0.0
Do You Want more Calculation Enter 'Y' for Yes and any key for No =
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at zip.Calculater.addition(Calculater.java:85)
at zip.Calculater.main(Calculater.java:40)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
使用IO.next();
尝试不同的方法,在转移到下一行代码之前,会等待输入。
我会建议这样的事情:
package zip;
import java.util.Scanner;
public class Calculater
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Enter \'+\' for addition.");
if(!console.next().equals("+"))
{
System.out.println(console.next() + "is not an accepted character.");
System.exit(0);
}
else
{
System.out.println("Enter first number followed enter then the second number followed by enter.");
String f = console.next();
String s = console.next();
int fI = Integer.parseInt(f);
int sI = Integer.parseInt(s);
System.out.println(fI + " + " + sI + "=" + (fI + sI));
}
}
}
输出:
Enter '+' for addition.
+
Enter first number followed enter then the second number followed by enter.
56
23
56 + 23=79
BUILD SUCCESSFUL (total time: 7 seconds)
也尽量不要使用大写字母作为对象名称,在开头使用大写字母表示对象名称,在开头使用小写字母作为对象名称。