简体   繁体   中英

How can I ask the user to re-enter their choice?

I know how to display an Error message if the user enters a number below 10 or higher than 999 but how can I code to make sure the program doesn't end after the users enter a number below 10 or higher than 999 and give them a second chance to enter their valid input over and over again until they give a correct input.

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);
}
}
}

You will need to use a loop, which basically, well, loops around your code until a certain condition is met.

A simple way to do this is with a do/while loop. For the example below, I will use what's called an "infinite loop." That is, it will continue to loop forever unless something breaks it up.

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);

    }
}

Another method, as suggested by MadProgrammer, is to use a do/while loop. For this example, I've also added some validation to ensure the user enters a valid integer, thus avoiding some Exceptions:

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);

    }
}

Try this, i just include the while loop in your code it will work fine.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM