首页 > 解决方案 > 如何限制 Java 中的“新 Scanner(System.in)”系统,不进一步处理用户输入数据,除非它是正确的输入值?

问题描述

我正在学习 Java,并且我的知识有所进步。我希望应用程序在用户输入的值正确的情况下更进一步,否则我希望相同的问题(控制台输入)自我重复(而不是进一步使用应用程序),直到用户输入正确价值!我的代码如下所示:

package oop.project;

import java.util.Scanner;

/**
 *
 * @author Dragos
 */
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);
}

构造函数类及其功能:

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) {
        String input = in.nextLine();
        do{
            isEngineOn = true; 
        } while(input.equals("Yes") || input.equals("Start") || input.equals("Turn on"));

        if(isEngineOn) {
            System.out.println("Engine is on!");
        } else {
            System.out.println("Your input is not correct! Please start the engine!");
            isEngineOn = false;
        }  
        return input;
    }

在 do & while 之前,我只有 if-else 语句和(input.equals("Yes") || input.equals("Start") || input.equals("Turn on"))if 语句中的消息,当输入错误的单词时,但不会停止或限制用户输入正确的值!尝试通过添加 do-while 语句来限制用户输入正确的值,但效果不佳。

标签: java

解决方案


在获得有效输入之前,您必须处于循环状态。试试下面的片段。

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

如果你想从函数返回输入,你可以像下面那样做。

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

推荐阅读