Java 月食“;scanner.ensureopen()行不可用;
我正在学习使用面向对象,我被这个错误所困扰,当eclipse到达我的主类的最后一个类时,它会把我抛出eclipse。问题是,我有一个名为Wheel的类,我想存储与两个后轮和两个前轮相关的用户输入,然后只显示我存储在console中的全部信息 在到达Java 月食“;scanner.ensureopen()行不可用;,java,Java,我正在学习使用面向对象,我被这个错误所困扰,当eclipse到达我的主类的最后一个类时,它会把我抛出eclipse。问题是,我有一个名为Wheel的类,我想存储与两个后轮和两个前轮相关的用户输入,然后只显示我存储在console中的全部信息 在到达frontwheels的最后一个扫描仪之前,它一直工作,我在控制台中为它写入输入,它抛出“scanner.ensurereopen()行不可用”错误,在eclipse的另一个窗口中打开它,程序没有向我显示frontwheels的信息,并且没有按应有的方
frontwheels
的最后一个扫描仪之前,它一直工作,我在控制台中为它写入输入,它抛出“scanner.ensurereopen()行不可用”
错误,在eclipse的另一个窗口中打开它,程序没有向我显示frontwheels
的信息,并且没有按应有的方式完成。
这是我的密码:
轮级
package com.vehicles.project;
public class Wheel {
private String brand;
private double diameter;
// Wheel Constructor
public Wheel(String brand, double diameter) {
this.brand = brand;
this.diameter = diameter;
}
public String infoWheel() {
return "brand: " + brand + " and diameter: " + diameter;
}
}
主要
我没有遇到指定的错误OP,通常在关闭扫描仪时会出现此错误。根据OP的代码,它在主类中似乎没有关闭
scanner.ensureopen() line not available
我遇到了不同的错误
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at com.vehicles.project.Main_Vehicles_Fase1.main(Main_Vehicles_Fase1.java:28)
我改变了双重逻辑。你能试试吗。操作码并不完美。我不想改进它,因为它可能是一个家庭作业
package com.vehicles.project;
import java.util.Scanner;
public class Main_Vehicles_Fase1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your plate, car brand and color");
Car userCar = new Car(sc.nextLine(), sc.nextLine(), sc.nextLine());
System.out.println("Your plate is: " + userCar.plate + ", the brand is " + userCar.brand + " and the colour is "
+ userCar.color);
System.out.println("Enter backwheels brand and diameter");
Wheel userBackWheels = new Wheel(sc.nextLine(), new Double(sc.nextLine()));
System.out.println("Your backwheels info --> " + userBackWheels.infoWheel());
System.out.println("Enter frontwheels brand and diameter");
Wheel userFrontWheels = new Wheel(sc.nextLine(), new Double(sc.nextLine()));
System.out.println("Your frontwheels info --> " + userFrontWheels.infoWheel());
sc.close();
}
}
这是你的真实代码吗?这段代码是否产生了您描述的错误?您是否关闭了另一个
扫描器(System.in)
扫描器在代码中的任何其他位置?它不是完整的代码,我有另一个名为Car和另一个名为vehicle的类,但问题只在于wheel类,所以我只是复制了该类和main。我只是在main的最后关闭了扫描仪,没有其他关闭。如果你需要帮助,发布一些我们可以复制的东西。这将是最好的。它有效!谢谢但我想知道,如果可能的话,你为什么要加上“新双人”,谢谢@Jaykooh java.util.Scanner.nextDouble()方法将输入的下一个标记扫描为双精度。如果无法将下一个标记转换为有效的双精度值,则此方法将抛出InputMismatchException。如果翻译成功,扫描仪将通过匹配的输入。提到
package com.vehicles.project;
import java.util.Scanner;
public class Main_Vehicles_Fase1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your plate, car brand and color");
Car userCar = new Car(sc.nextLine(), sc.nextLine(), sc.nextLine());
System.out.println("Your plate is: " + userCar.plate + ", the brand is " + userCar.brand + " and the colour is "
+ userCar.color);
System.out.println("Enter backwheels brand and diameter");
Wheel userBackWheels = new Wheel(sc.nextLine(), new Double(sc.nextLine()));
System.out.println("Your backwheels info --> " + userBackWheels.infoWheel());
System.out.println("Enter frontwheels brand and diameter");
Wheel userFrontWheels = new Wheel(sc.nextLine(), new Double(sc.nextLine()));
System.out.println("Your frontwheels info --> " + userFrontWheels.infoWheel());
sc.close();
}
}