我如何要求用户重新输入他们的选择?

时间:2019-03-22 03:56:23

标签: java

如果用户输入的数字小于10或大于999,我知道如何显示错误消息,但是我如何编码以确保程序在用户输入的数字小于10或大于999且不结束后又结束给他们第二次机会一次又一次地输入有效输入,直到他们提供正确的输入为止。

import java.util.Scanner; 

public class Ex1{ 

public static void main(String args[]){ 

java.util.Scanner input = new java.util.Scanner(System.in); 

System.out.print("Enter an integer between 10 and 999: "); 
    int number = input.nextInt(); 


    int lastDigit = number % 10; 

    int remainingNumber = number / 10; 
    int secondLastDigit = remainingNumber % 10; 
    remainingNumber = remainingNumber / 10; 
    int thirdLastDigit = remainingNumber % 10; 

int sum = lastDigit + secondLastDigit + thirdLastDigit; 

if(number<10 || number>999){
    System.out.println("Error!: "); 
}else{
    System.out.println("The sum of all digits in " +number + " is " + sum);
}
}
}

2 个答案:

答案 0 :(得分:1)

您将需要使用一个循环,基本上,该循环会在您的代码周围循环,直到满足特定条件为止。

一个简单的方法是使用do/while循环。对于下面的示例,我将使用所谓的“无限循环”。也就是说,除非有什么破坏,它将永远循环下去。

import java.util.Scanner;

class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int num;

        // Start a loop that will continue until the user enters a number between 1 and 10
        while (true) {

            System.out.println("Please enter a number between 1 - 10:");
            num = scanner.nextInt();

            if (num < 1 || num > 10) {
                System.out.println("Error: Number is not between 1 and 10!\n");
            } else {
                // Exit the while loop, since we have a valid number
                break;
            }
        }

        System.out.println("Number entered is " + num);

    }
}

MadProgrammer建议的另一种方法是使用do/while循环。对于此示例,我还添加了一些验证以确保用户输入有效的整数,从而避免了某些异常:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int num;

        // Start the loop
        do {

            System.out.println("Please enter a number between 1 - 10:");

            try {
                // Attempt to capture the integer entered by the user. If the entry was not numeric, show
                // an appropriate error message.
                num = Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("Error: Please enter only numeric characters!");
                num = -1;

                // Skip the rest of the loop and return to the beginning
                continue;
            }

            // We have a valid integer input; let's make sure it's within the range we wanted.
            if (num < 1 || num > 10) {
                System.out.println("Error: Number is not between 1 and 10!\n");
            }

            // Keep repeating this code until the user enters a number between 1 and 10
        } while (num < 1 || num > 10);

        System.out.println("Number entered is " + num);

    }
}

答案 1 :(得分:0)

尝试一下,我只是在您的代码中包含while循环,它将可以正常工作。

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int number = askInput(input); 

    while(number<10 || number>999) {
        System.out.println("Sorry Try again !");
        number = askInput(input);
    }
    int lastDigit = number % 10; 

    int remainingNumber = number / 10; 
    int secondLastDigit = remainingNumber % 10; 
    remainingNumber = remainingNumber / 10; 
    int thirdLastDigit = remainingNumber % 10; 

    int sum = lastDigit + secondLastDigit + thirdLastDigit; 

    if(number<10 || number>999){
        System.out.println("Error!: "); 
    }else{
        System.out.println("The sum of all digits in " +number + " is " + sum);
    }
}

private static int askInput(Scanner input) {
    int number = input.nextInt();
    return number;
}