SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.awt.*; | |
| 2 | import java.util.*; | |
| 3 | ||
| 4 | - | public static final Scanner CONSOLE = new Scanner(System.in); |
| 4 | + | |
| 5 | public static final Scanner CONSOLE = new Scanner(System.in); | |
| 6 | - | mEthod(); |
| 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 | - | |
| 14 | + | |
| 15 | String colorStr = CONSOLE.nextLine(); | |
| 16 | ||
| 17 | // If they input a valid input, return it | |
| 18 | if (colorStr.equals("red") || colorStr.equals("blue")) {
| |
| 19 | - | |
| 19 | + | |
| 20 | - | // If we haven't returned yet, their choice was invalid. |
| 20 | + | |
| 21 | - | System.out.println(colorStr + "is an invalid choice."); |
| 21 | + | |
| 22 | // If we haven't returned yet, their choice was invalid. | |
| 23 | - | |
| 23 | + | System.out.println(colorStr + " is an invalid choice."); |
| 24 | } | |
| 25 | ||
| 26 | - | throw new Exception("Invalid input!");
|
| 26 | + | |
| 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 | - | System.out.println(sizeStr + "is an invalid choice."); |
| 41 | + | |
| 42 | // If we haven't returned yet, their choice was invalid. | |
| 43 | System.out.println(sizeStr + " is an invalid choice."); | |
| 44 | } | |
| 45 | ||
| 46 | - | throw new Exception("Invalid input!");
|
| 46 | + | |
| 47 | System.out.println("No valid input after two tries!");
| |
| 48 | - | |
| 48 | + | throw new Exception(); |
| 49 | - | public static void mEthod() {
|
| 49 | + | |
| 50 | - | //read in the color |
| 50 | + | |
| 51 | public static void mEthod() {
| |
| 52 | Color c = null; | |
| 53 | - | |
| 53 | + | |
| 54 | ||
| 55 | try {
| |
| 56 | //read in the color | |
| 57 | - | |
| 57 | + | |
| 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 | - | |
| 64 | + | |
| 65 | } | |
| 66 | ||
| 67 | - | |
| 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 | } |