首页 > 解决方案 > Java Scanner 输入和 while 循环出现问题,其中包含多个 if 语句

问题描述

我希望使用 while 循环在某些值之间限制整数值的输入,其中包含几个 if-else if-else 语句!它有点工作,但不完全应该......也想过使用开关,但我太“绿色”不知道怎么做!如果有人愿意并且知道如何......我也欢迎使用开关!如果需要,甚至是嵌套开关......这是我的代码:

public class OOPProject {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    Car Honda = new Car(2018, 20000, "Honda", "Civic", 200, 6, 0, 0);
    System.out.println("Manufacturer is: " + Honda.maker + ", model: " + Honda.model + 
                       ", year of fabrication: " + Honda.year + ", price: " + Honda.price + "!");
    System.out.println("Please start the engine of your vehicle, by typing in 'Yes' or 'Start' or 'Turn on'!");
    System.out.print("Do you wish to start the engine?");
    System.out.println(" ");
    Honda.StartEngine(sc);
    //System.out.println("Engine is on!");
    System.out.println("Do you wish to depart? Shift in to the first gear then and accelerate!");
    System.out.println("Type in the speed: ");
    Honda.accelerate(sc);
    System.out.println("We are departing! Shifting in to " + Honda.currentGear + 
                       "st gear and accelerating to " + Honda.currentSpeed + " km per hour!");
}

构造函数和函数:

public class Car {

    public int year;
    public int price;
    public String maker;
    public String model;

    public int maximumSpeed;
    public int numberOfGears;
    public int currentSpeed;
    public int currentGear;
    public boolean isEngineOn;

    public Car(int year, int price, String maker, String model, int maximumSpeed,
            int numberOfGears, int currentSpeed, int currentGear) {
        this.year = year;
        this.price = price;
        this.maker = maker;
        this.model = model;
        this.maximumSpeed = maximumSpeed;
        this.numberOfGears = numberOfGears;
        this.currentSpeed = currentSpeed;
        this.currentGear = currentGear;
    }

    public String StartEngine(Scanner in) {

        while(in.hasNext()) {
            String input = in.nextLine();
            if(input.equals("Yes") || input.equals("Start") || input.equals("Turn on")) {
                isEngineOn = true;
                System.out.println("Engine is on!");
                return input;
            } else {
                System.out.println("Your input is not correct! Please start the engine!");
            }
        }
        return null;
    }

public int accelerate(Scanner in){
        while(in.hasNextInt()){
            currentSpeed = in.nextInt();
            if(isEngineOn && currentSpeed > 0){
                currentGear++;
            } else if(currentSpeed > 50){
                System.out.println("We cannot accelerate to more than 50 km per hour, when shifting in the 1st gear!");
            } else{
                System.out.println("We cannot depart at 0 km per hour!");
            }
        }
        return 0;
    }
}

它正在接受输入,但它并没有像它应该的那样走得更远,它也没有给出错误消息或停止应用程序,我的错误是什么?

标签: java

解决方案


更改 if 语句的顺序将起作用。

在您当前的方法中:

if(isEngineOn && currentSpeed > 0)

将始终使用您输入的任何值返回 true。

使用这种方法会让你走得更远,虽然我怀疑它仍然不会是你所期望的,但我希望它可以帮助你朝着正确的方向前进。

public int accelerate(Scanner in){
    while(in.hasNextInt()){
        currentSpeed = in.nextInt();
        if(currentSpeed > 50 && currentGear <= 1){
            System.out.println("We cannot accelerate to more than 50 km per hour, when shifting in the 1st gear!");
        } else if(isEngineOn && currentSpeed > 0){
          currentGear++;
          break; /* I've added this to break out of the method to progress in your flow */
        } else{
            System.out.println("We cannot depart at 0 km per hour!");
        }
      }
      return 0;
   }
}

推荐阅读