Java “如何修复”;“非接触性异常”;被扔
我创建了一个交互式类,用于解决运动学方程问题(在用户输入循环中的另一个类:mathiverse中访问),该类工作正常,但在给出答案后,它会抛出一个Java “如何修复”;“非接触性异常”;被扔,java,nosuchelementexception,Java,Nosuchelementexception,我创建了一个交互式类,用于解决运动学方程问题(在用户输入循环中的另一个类:mathiverse中访问),该类工作正常,但在给出答案后,它会抛出一个NoTouchElementException 我试着移动我关闭扫描仪的地方,但它没有起作用,我很确定问题在于我的运动学类如何在我的输入循环中运行 我的用户输入循环和运动学构造函数(不在同一类中): 给出答案后,我希望我的代码继续搜索输入,但它会抛出以下消息: 线程“main”java.util.NoSuchElementException中的异常:无
NoTouchElementException
我试着移动我关闭扫描仪的地方,但它没有起作用,我很确定问题在于我的运动学类如何在我的输入循环中运行
我的用户输入循环和运动学构造函数(不在同一类中):
给出答案后,我希望我的代码继续搜索输入,但它会抛出以下消息:
线程“main”java.util.NoSuchElementException中的异常:无行
可在以下位置找到java.base/java.util.Scanner.nextLine(Scanner.java:1651)
main(Mathiverse.java:53)
从运动学关闭扫描仪时,也将关闭System.in stream。在主方法和运动学中使用相同的扫描仪 看看:
while(!(input.equals("exit")))
{
if(input.equals("help"))
{
System.out.println("~ Commands ~");
System.out.println("help - brings up a list of
commands(what you're reading)");
System.out.println("kinematic - solves a kinematic
equations problem; requires input of known and unknown
variables");
System.out.println("exit - closes the program");
System.out.println("~~~~~~~~~~~~");
}
//user decides to explore kinematic options
if(input.equals("kinematic"))
{
Kinematic calc = new Kinematic();
System.out.println(calc.answer());
}
input = scan.nextLine();
}
public Kinematic()
{
Scanner scanMath = new Scanner(System.in);
System.out.println("If you are solving for the variable, enter \"?\",
if the variable is not given, enter a space.");
System.out.println("Enter the acceleration: ");
acc = scanMath.nextLine();
System.out.println("Enter the displacement: ");
disp = scanMath.nextLine();
System.out.println("Enter the initial velocity: ");
init = scanMath.nextLine();
System.out.println("Enter the final velocity: ");
fin = scanMath.nextLine();
System.out.println("Enter the time: ");
time = scanMath.nextLine();
scanMath.close();
}