Advertisement
kyle1320

Untitled

Oct 27th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.util.*;
  3. public class TestPart1 {
  4. public static final Scanner CONSOLE = new Scanner(System.in);
  5.   public static void main(String args[]){
  6.     mEthod();
  7.   }
  8.  
  9.   public static String getColorInput() throws Exception{
  10.     // give the user two tries
  11.     for (int i=0; i < 2; i++) {
  12.       System.out.println("Enter a color for the ball (red or blue):");
  13.       String colorStr = CONSOLE.nextLine();
  14.      
  15.       // If they input a valid input, return it
  16.       if (colorStr.equals("red") || colorStr.equals("blue")) {
  17.         return colorStr;
  18.       }
  19.      
  20.       // If we haven't returned yet, their choice was invalid.
  21.       System.out.println(colorStr + "is an invalid choice.");
  22.     }
  23.    
  24.     // If we get outside of the loop, the user did not input a valid choice. Throw an exception to be caught higher up.
  25.     System.out.println("No valid input after two tries!");
  26.     throw new Exception("Invalid input!");
  27.   }
  28.  
  29.   public static int getSizeInput() throws Exception{
  30.     // give the user two tries
  31.     for (int i=0; i < 2; i++) {
  32.       System.out.println("Enter the size of the ball (3 to 200):");
  33.       int sizeStr = CONSOLE.nextInt();
  34.      
  35.       // If they input a valid input, return it
  36.       if (sizeStr >= 3 && sizeStr <= 200) {
  37.         return sizeStr;
  38.       }
  39.      
  40.       // If we haven't returned yet, their choice was invalid.
  41.       System.out.println(sizeStr + "is an invalid choice.");
  42.     }
  43.    
  44.     // If we get outside of the loop, the user did not input a valid choice. Throw an exception to be caught higher up.
  45.     System.out.println("No valid input after two tries!");
  46.     throw new Exception("Invalid input!");
  47.   }
  48.  
  49.   public static void mEthod() {
  50.     //read in the color
  51.     Color c = null;
  52.     int s = 0;
  53.    
  54.     try {
  55.       String colorStr = getColorInput();
  56.       System.out.println("The bouncing ball will be " + colorStr + ".");
  57.      
  58.       // Check which color they entered, and set c to the corresponding value
  59.       if (colorStr.equals("red")){
  60.         c = Color.RED;
  61.       }else {
  62.         c = Color.BLUE;
  63.       }
  64.      
  65.       int sizeStr = getSizeInput();
  66.       System.out.println("The size of the bouncing ball will be " + sizeStr + ".\n");
  67.      
  68.       s = sizeStr;
  69.     } catch (Exception e) {
  70.       System.exit(0);
  71.     }
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement