Advertisement
Guest User

TEST

a guest
Aug 28th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. package com.HelloWorld;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Test
  6. {
  7.     public static void main(String[] tArgs)
  8.     {
  9.         Scanner input = new Scanner(System.in);
  10.         System.out.println("What is your name?");
  11.         String name = input.nextLine();
  12.         String greeting = "Hello " + name;
  13.         System.out.println(greeting);
  14.         System.out.println("");
  15.         System.out.println("Do you want to exit? 1 = yes 0 = no");
  16.         int num = nextValidInt(-1, 2);
  17.         if (num == 1)
  18.         {
  19.             System.out.println("Quitting");
  20.         }
  21.         else if (num == 0)
  22.         {
  23.             System.out.println("Quitting anyway!");
  24.         }
  25.  
  26.     }
  27.  
  28.     public static int nextValidInt(int min, int max)
  29.     {
  30.         //create a scanner
  31.         Scanner myScanner = new Scanner(System.in);
  32.         //create output, set it to zero
  33.         int output = 0;
  34.         //initiate the valid boolean
  35.         boolean valid = false;
  36.         do
  37.         {
  38.             //request the users input
  39.             System.out.println("Enter an integer between " + min + " and " + max);
  40.             //user inputs now
  41.             output = myScanner.nextInt();
  42.             //is the output valid?
  43.             if (output >= min && output <= max)
  44.             {
  45.                 valid = true;
  46.             }
  47.  
  48.  
  49.         } while (!valid);
  50.         //returns a validated output
  51.         return output;
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement