我正在学习使用面向对象的方法,并且我陷入了这个错误,当它到达我的主要班级的最后阶段时,我就把日食弄丢了。事实是,我有一个名为Wheel的类,我想存储与两个后轮以及两个前轮有关的用户输入,然后仅显示存储在控制台中的全部信息。
它完全可以工作,直到到达frontwheels
的最后一个扫描器为止,我在控制台中为其输入输入,并抛出" scanner.ensureopen() line not available "
错误,在另一个Eclipse窗口中将其打开,并且该程序没有没有向我显示frontwheel
的信息,也没有按要求完成。
这是我的代码:
车轮类
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;
}
}
主要
package com.vehicles.project;
import java.util.Scanner;
import javax.swing.*;
import com.vehicles.project.*;
public class Main_Vehicles_Fase1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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(), sc.nextDouble());
System.out.println("Your backwheels info --> " + userBackWheels.infoWheel());
System.out.println("Enter frontwheels brand and diameter");
Wheel userFrontWheels = new Wheel(sc.nextLine(), sc.nextDouble());
System.out.println("Your frontwheels info --> " + userFrontWheels.infoWheel());
sc.close();
}
答案 0 :(得分:1)
我没有遇到指定的错误OP,通常如果关闭扫描仪,我们会收到此错误。根据OP的代码,它似乎没有在主类中关闭。
export default async function getCountries() {
return await (await fetch("https://restcountries.eu/rest/v2/all")).json();
}
我遇到了其他错误
scanner.ensureopen() line not available
我更改了解析双重逻辑。你可以尝试一下。 OP代码并不完美。我不想改进它,因为这可能是一项家庭作业。
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)