NEWS: Total Picture Views: 155282 | Total Article Views: 165613 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. PickUpThings with Becker Robot | 3. How to read a JSON and work with it | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Random Number with loop and exception handling Java


In this example I will be showing you how to randomize a number while using a do loop and exception handling


import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int x,max,min,answer;
        boolean quit;

        Scanner input = new Scanner(;
                System.out.print("Enter the minimum value:  ");
                //If the user enter something other then integer it will go in the catch exception.
                min = input.nextInt();

                System.out.print("Enter the maximum value:  ");
                //If the user enter something other then integer it will go in the catch exception.
                max = input.nextInt();

                Random rnd = new Random();

                answer = rnd.nextInt((max - min) + min);

                do {
                    System.out.print("Find the random number between " + min + " and " + max + ": ");
                    x = input.nextInt();

                    if (x == answer){
                        System.out.println("Correct answer!");
                        System.out.println("Bad answer!");
                }while(x != answer);

                System.out.print("Do you wan to continue? (True or False): ");
                quit = input.nextBoolean();
            }while(quit != false);
        }catch (Exception e){


Attachments: None

Tags: Random Random number do loop Exception Exception handling

Total Views: 1047

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 09/04/14


Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.