SHOW:
|
|
- or go back to the newest paste.
| 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{
|
| 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 the corresponding value | |
| 16 | if (colorStr.equals("red") || colorStr.equals("blue")) {
| |
| 17 | - | return Color.RED; |
| 17 | + | return colorStr; |
| 18 | - | }else if (colorStr.equals("blue")){
|
| 18 | + | |
| 19 | - | return Color.BLUE; |
| 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 void mEthod() {
| |
| 30 | //read in the color | |
| 31 | Color c = null; | |
| 32 | try {
| |
| 33 | - | Color c; |
| 33 | + | String colorStr = getColorInput(); |
| 34 | System.out.println("The bouncing ball will be " + colorStr + ".");
| |
| 35 | - | c = getColorInput(); |
| 35 | + | |
| 36 | if (colorStr.equals("red")){
| |
| 37 | c = Color.RED; | |
| 38 | }else {
| |
| 39 | c = Color.BLUE; | |
| 40 | - | System.out.println("The bouncing ball will be " + colorStr + ".\n");
|
| 40 | + | |
| 41 | } catch (Exception e) {
| |
| 42 | System.exit(0); | |
| 43 | } | |
| 44 | ||
| 45 | System.out.println("Enter the size of the ball (3 to 200):");
| |
| 46 | int sizeStr = CONSOLE.nextInt(); | |
| 47 | ||
| 48 | if ( sizeStr >=3 && sizeStr <=200){
| |
| 49 | int s = sizeStr; | |
| 50 | if(sizeStr <3 && sizeStr >200){
| |
| 51 | System.out.println( sizeStr +" is an invalid choice\nEnter the size of the ball (3 to 200):"); | |
| 52 | sizeStr = CONSOLE.nextInt(); | |
| 53 | } else{
| |
| 54 | if (sizeStr <3 && sizeStr >200){
| |
| 55 | System.out.println( sizeStr + " is an invalid choic\nNo correct input after 2 inputs"); | |
| 56 | throw new IllegalArgumentException(); | |
| 57 | //System.exit(0); | |
| 58 | } else{
| |
| 59 | s = sizeStr; | |
| 60 | ||
| 61 | ||
| 62 | System.out.println("The size of the bouncing ball will be " + sizeStr+".\n");
| |
| 63 | } | |
| 64 | } | |
| 65 | } | |
| 66 | } | |
| 67 | } |