“线程中的异常”;“主要”;java.util.NoSuchElementException运行时错误
我很不擅长面向对象编程,我需要帮助解决导致此运行时错误的原因。我知道这可能很简单,所以请不要因为我的无能而生我的气。谢谢你抽出时间 代码如下:“线程中的异常”;“主要”;java.util.NoSuchElementException运行时错误,java,Java,我很不擅长面向对象编程,我需要帮助解决导致此运行时错误的原因。我知道这可能很简单,所以请不要因为我的无能而生我的气。谢谢你抽出时间 代码如下: import static java.lang.System.out; import java.util.Scanner; class FormulaMethods { void Force() { double mass, acceleration, answer; Scanner input = new Scanner(Syst
import static java.lang.System.out;
import java.util.Scanner;
class FormulaMethods {
void Force() {
double mass, acceleration, answer;
Scanner input = new Scanner(System.in);
out.println("You have chosen the F=MA (Find Force) Formula.");
out.println("Enter Mass Value: ");
mass = input.nextDouble();
out.println("Enter acceleration value: ");
acceleration = input.nextDouble();
answer = mass * acceleration;
out.printf("The Force of these given variables is: %lf", answer);
}
}
及
没有编译错误,但我确实得到了以下信息:
线程“main”java.util.NoSuchElementException中出现异常
位于java.util.Scanner.throwFor(未知源)
位于java.util.Scanner.next(未知源)
位于java.util.Scanner.nextInt(未知源)
位于java.util.Scanner.nextInt(未知源)
位于FormulaCalculator.main(FormulaCalculator.java:7)
有什么建议吗?由于没有数据,导致此异常的原因是扫描仪类方法nextInt。首先使用Scanner类方法hasNext()检查数据是否可用 现在我得到了这个错误:java.util.Scanner.throwFor(未知源)java.util.Scanner.next(未知源)java.util.Scanner.nextInt(未知源)java.util.Scanner.nextInt(未知源)FormulaCalculator.main的java.util.Scanner.nextInt(未知源)java.util.Scanner.nextInt(未知源)线程“main”中的异常(FormulaCalculator.java:7)您可能正在提供非整数输入(可能是新行或其他内容)nextInt不接受的。对nextInt行使用try-catch可能会有所帮助。不,比如,编译后在控制台中键入java FormulaCalculator时,该程序根本不会运行。它只是在我键入某个内容后才执行任何操作,然后打印出上面的错误。nvm,计算出了它。结果是,它在打印initi之前正在扫描我是个傻瓜。
import java.util.Scanner;
import static java.lang.System.out;
class FormulaCalculator {
public static void main(String args[]) {
Scanner choice = new Scanner(System.in);
int formula = choice.nextInt();
FormulaMethods form = new FormulaMethods();
System.out.println("Welcome to the Physics Formula Calculator Tool!");
System.out.println("Please choose the number for the formula you would like to calculate: ");
switch (formula) {
case 1:
form.Force();
break;
default:
System.out.println("No such option even exists, dimwit.");
break;
}
}
}