SUPER初学者程序员,我正处于基于java的编程类入门的开头。我正在使用扫描仪创建一个平均加速度计算器并需要一些帮助,因为我无法解释它为什么不起作用,但这里有多远:
import java.util.Scanner;
public class averageAcceleration {
public static void main(String[] args) {
// Scanner for Initial Velocity
Scanner in = new Scanner(System.in);
System.out.print("Enter Inital Velocity-->>");
int vI = in.nextInt();
in.close();
// Scanner for Final Velocity
Scanner in1 = new Scanner(System.in);
System.out.print("Enter Final Velocity-->>");
int vF = in1.nextInt();
in1.close();
//Scanner for Time in seconds
Scanner in2 = new Scanner(System.in);
System.out.print("Enter Time in Seconds-->>");
int tS = in2.nextInt();
in2.close();
double averageAcceleration = (vF - vI) / tS;
System.out.print("Inital Velocity is " + vI + "Final Velocity is " + vF + "Time is " + tS);
System.out.print("Average Acceleration is " + averageAcceleration);
答案 0 :(得分:0)
问题是,您在创建新扫描仪之前关闭扫描仪。当您close
扫描程序时,基础流也会关闭,在这种情况下它是System.in
。然后,您可以使用System.in
创建一个已关闭的新扫描程序。显然,创建一个封闭流的扫描仪不会起作用。
只需使用一台扫描仪即可完成所有操作,并且没有任何问题。
Scanner in = new Scanner(System.in);
System.out.print("Enter Inital Velocity-->>");
int vI = in.nextInt();
System.out.print("Enter Final Velocity-->>");
int vF = in.nextInt();
System.out.print("Enter Time in Seconds-->>");
int tS = in.nextInt();
double averageAcceleration = (vF - vI) / tS;
System.out.print("Inital Velocity is " + vI + "Final Velocity is " + vF + "Time is " + tS);
System.out.print("Average Acceleration is " + averageAcceleration);