Advertisement
kyle1320

Untitled

Oct 27th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  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 Color 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 the corresponding value
  16.       if (colorStr.equals("red")){
  17.         return Color.RED;
  18.       }else if (colorStr.equals("blue")){
  19.         return Color.BLUE;
  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("Invalid input!");
  29.   }
  30.  
  31.   public static void mEthod() {
  32.     //read in the color
  33.     Color c;
  34.     try {
  35.       c = getColorInput();
  36.     } catch (Exception e) {
  37.       System.exit(0);
  38.     }
  39.    
  40.     System.out.println("The bouncing ball will be " + colorStr + ".\n");
  41.  
  42.     System.out.println("Enter the size of the ball (3 to 200):");
  43.     int sizeStr = CONSOLE.nextInt();
  44.    
  45.     if ( sizeStr >=3 && sizeStr <=200){
  46.       int s = sizeStr;
  47.     if(sizeStr <3 && sizeStr >200){
  48.     System.out.println( sizeStr +" is an invalid choice\nEnter the size of the ball (3 to 200):");
  49.     sizeStr = CONSOLE.nextInt();
  50.     } else{
  51.     if (sizeStr <3 && sizeStr >200){
  52.       System.out.println( sizeStr + " is an invalid choic\nNo correct input after 2 inputs");
  53.       throw new IllegalArgumentException();
  54.     //System.exit(0);  
  55.     } else{
  56.         s = sizeStr;
  57.  
  58.    
  59.     System.out.println("The size of the bouncing ball will be " + sizeStr+".\n");
  60.   }
  61.     }
  62.     }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement