Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.HelloWorld;
- import java.util.Scanner;
- public class Test
- {
- public static void main(String[] tArgs)
- {
- Scanner input = new Scanner(System.in);
- System.out.println("What is your name?");
- String name = input.nextLine();
- String greeting = "Hello " + name;
- System.out.println(greeting);
- System.out.println("");
- System.out.println("Do you want to exit? 1 = yes 0 = no");
- int num = nextValidInt(-1, 2);
- if (num == 1)
- {
- System.out.println("Quitting");
- }
- else if (num == 0)
- {
- System.out.println("Quitting anyway!");
- }
- }
- public static int nextValidInt(int min, int max)
- {
- //create a scanner
- Scanner myScanner = new Scanner(System.in);
- //create output, set it to zero
- int output = 0;
- //initiate the valid boolean
- boolean valid = false;
- do
- {
- //request the users input
- System.out.println("Enter an integer between " + min + " and " + max);
- //user inputs now
- output = myScanner.nextInt();
- //is the output valid?
- if (output >= min && output <= max)
- {
- valid = true;
- }
- } while (!valid);
- //returns a validated output
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement