Java 扫描器限制整数 < 1 & 字符串输入

Java scanner restrict integer < 1 & string input

试图弄清楚如何让我的程序限制输入小于 1 的整数并限制扫描器中的字符串输入。这是我的代码:

import java.util.Scanner; // Import scanner object
import java.io.*; // Import for file and IOException

public class Distance {

    public static void main(String[] args) throws IOException {

        int distance;
        int speed, time;
        String filename;

        System.out.println("Welcome to Distance Calculator.");

        // Create a scanner keyboard for user input
        Scanner keyboard = new Scanner(System.in);

        // Vehicle speed
        System.out.print("Vehicle speed (MPH): ");
        speed = keyboard.nextInt();
        while (!keyboard.hasNextInt()) {
            System.out.print("Please enter a valid #: ");
            speed = keyboard.nextInt();
            if (speed < 1) {
                System.out.print("Please enter a # greater then 1: ");
                keyboard.nextInt();
            }
        }

        System.out.print("Time vehicle traveled (HR): ");
        while (!keyboard.hasNextInt()) {
            time = keyboard.nextInt();
            if (time < 1) {
                System.out.print("Please enter a valid time: ");
                speed = keyboard.nextInt();
            }
        }

        time = keyboard.nextInt();

        keyboard.nextLine(); // Consume next line

        // Get filename
        System.out.print("File name for saving: ");
        filename = keyboard.nextLine();

        // Open file
        String filePath = "C:/Users/Nik/Desktop/";
        PrintWriter outputFile = new PrintWriter(filePath + filename);

        outputFile.println("Hour        Distance Traveled");
        outputFile.println("-----------------------------");

        for (int hour = 1; hour <= time; hour++) {
            distance = (speed * hour);
            outputFile.println(hour + "\t\t\t" + (distance + " Mi"));
        }
        outputFile.close();
        System.out.print("Date written to " + filePath + filename);
    }
}

非常感谢您的帮助。

我认为这应该可行:

System.out.print("Vehicle speed (MPH): ");
speed = -1;
do {
    System.out.println("Please enter a valid integer greater than 1");
    if (keyboard.hasNextInt() {
        speed = keyboard.nextInt();
    }
} while (speed < 1)

我很确定问题出在您的 while 循环中的 !,但是我想我已经设法清理了代码。

免责声明:我没有测试此代码是否有效,但我想我会试一试,希望它有所帮助。

好吧,我相信稍微像这样更改 "whiles" 可能会如您所愿。当您插入非整数时,它会处理输入问题;当插入非正整数时,它会处理正整数问题。我测试了它,我认为它按预期工作。试试吧。

public static void main(String[] args) throws IOException{
    int distance;
    int speed, time;
    String filename;

    System.out.println("Welcome to Distance Calculator.");

    // Create a scanner keyboard for user input
    Scanner keyboard = new Scanner(System.in);

    // Vehicle speed
    System.out.print("Vehicle speed (MPH): ");


    while (!keyboard.hasNextInt() || 
            ((speed = keyboard.nextInt()) < 1) ) {
        System.out.print("Please enter a valid #: ");
        keyboard.nextLine();
    }

    System.out.print("Time vehicle traveled (HR): ");

    while (!keyboard.hasNextInt() || 
            ((time = keyboard.nextInt()) < 1) ) {
        System.out.print("Please enter a valid #: ");
        keyboard.nextLine();
    }

    keyboard.nextLine(); // Consume next line

    // Get filename
    System.out.print("File name for saving: ");
    filename = keyboard.nextLine();

    // Open file
    String filePath = "C:/Users/Nik/Desktop/";
    PrintWriter outputFile = new PrintWriter(filePath + filename);

    outputFile.println("Hour        Distance Traveled");
    outputFile.println("-----------------------------");

    for (int hour = 1; hour <= time; hour++) {
        distance = (speed * hour);
        outputFile.println(hour + "\t\t\t" + (distance + " Mi"));
    }
    outputFile.close();
    System.out.print("Date written to " + filePath + filename);
}

我的最终代码:

int distance;
        int speed = 0, time;
        String filename;
        boolean validInput = false; // Boolean for validating scanner input

        System.out.println("Welcome to Distance Calculator.");

        // Create a scanner keyboard for user input
        Scanner keyboard = new Scanner(System.in);

        // Vehicle speed
        System.out.print("Vehicle speed (MPH): ");

        // Method for validating user input
        while (validInput == false) {
            if (!keyboard.hasNextInt()) { // check if keyboard scanner !integer
                System.out.print("Please enter a valid #: "); // prompts user for valid input
                keyboard.nextLine(); // consumes next line
            }
            else {
                speed = keyboard.nextInt();
                if (speed < 1) { // validates if speed > 0
                    System.out.print("Please enter a value greater then 1: "); // prompts user for valid speed
                    keyboard.nextLine(); // consumes next line
                }
                else validInput = true; // if statements are passed then set bool to True and end loop
            }
        }