Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.*;
- public class TestPart1 {
- public static final Scanner CONSOLE = new Scanner(System.in);
- public static void main(String args[]){
- mEthod();
- }
- public static Color getColorInput() throws Exception{
- // give the user two tries
- for (int i=0; i < 2; i++) {
- System.out.println("Enter a color for the ball (red or blue):");
- String colorStr = CONSOLE.nextLine();
- // If they input a valid input, return the corresponding value
- if (colorStr.equals("red")){
- return Color.RED;
- }else if (colorStr.equals("blue")){
- return Color.BLUE;
- }
- // If we haven't returned yet, their choice was invalid.
- System.out.println(colorStr + "is an invalid choice.");
- }
- // If we get outside of the loop, the user did not input a valid choice. Throw an exception to be caught higher up.
- System.out.println("No valid input after two tries!");
- throw new Exception("Invalid input!");
- }
- public static void mEthod() {
- //read in the color
- Color c;
- try {
- c = getColorInput();
- } catch (Exception e) {
- System.exit(0);
- }
- System.out.println("The bouncing ball will be " + colorStr + ".\n");
- System.out.println("Enter the size of the ball (3 to 200):");
- int sizeStr = CONSOLE.nextInt();
- if ( sizeStr >=3 && sizeStr <=200){
- int s = sizeStr;
- if(sizeStr <3 && sizeStr >200){
- System.out.println( sizeStr +" is an invalid choice\nEnter the size of the ball (3 to 200):");
- sizeStr = CONSOLE.nextInt();
- } else{
- if (sizeStr <3 && sizeStr >200){
- System.out.println( sizeStr + " is an invalid choic\nNo correct input after 2 inputs");
- throw new IllegalArgumentException();
- //System.exit(0);
- } else{
- s = sizeStr;
- System.out.println("The size of the bouncing ball will be " + sizeStr+".\n");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement