Advertisement
kyle1320

Untitled

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