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 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 |
| 15 | + | // If they input a valid input, return it |
| 16 | if (colorStr.equals("red") || colorStr.equals("blue")) {
| |
| 17 | return colorStr; | |
| 18 | } | |
| 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 int getSizeInput() throws Exception{
| |
| 30 | // give the user two tries | |
| 31 | for (int i=0; i < 2; i++) {
| |
| 32 | System.out.println("Enter the size of the ball (3 to 200):");
| |
| 33 | int sizeStr = CONSOLE.nextInt(); | |
| 34 | ||
| 35 | // If they input a valid input, return it | |
| 36 | if (sizeStr >= 3 && sizeStr <= 200) {
| |
| 37 | return sizeStr; | |
| 38 | } | |
| 39 | ||
| 40 | // If we haven't returned yet, their choice was invalid. | |
| 41 | System.out.println(sizeStr + "is an invalid choice."); | |
| 42 | } | |
| 43 | ||
| 44 | // If we get outside of the loop, the user did not input a valid choice. Throw an exception to be caught higher up. | |
| 45 | - | System.out.println("Enter the size of the ball (3 to 200):");
|
| 45 | + | |
| 46 | - | int sizeStr = CONSOLE.nextInt(); |
| 46 | + | |
| 47 | } | |
| 48 | - | if ( sizeStr >=3 && sizeStr <=200){
|
| 48 | + | |
| 49 | - | int s = sizeStr; |
| 49 | + | |
| 50 | - | if(sizeStr <3 && sizeStr >200){
|
| 50 | + | |
| 51 | - | System.out.println( sizeStr +" is an invalid choice\nEnter the size of the ball (3 to 200):"); |
| 51 | + | |
| 52 | - | sizeStr = CONSOLE.nextInt(); |
| 52 | + | int s = 0; |
| 53 | - | } else{
|
| 53 | + | |
| 54 | - | if (sizeStr <3 && sizeStr >200){
|
| 54 | + | |
| 55 | - | System.out.println( sizeStr + " is an invalid choic\nNo correct input after 2 inputs"); |
| 55 | + | |
| 56 | - | throw new IllegalArgumentException(); |
| 56 | + | |
| 57 | - | //System.exit(0); |
| 57 | + | |
| 58 | - | } else{
|
| 58 | + | // Check which color they entered, and set c to the corresponding value |
| 59 | - | s = sizeStr; |
| 59 | + | |
| 60 | c = Color.RED; | |
| 61 | }else {
| |
| 62 | - | System.out.println("The size of the bouncing ball will be " + sizeStr+".\n");
|
| 62 | + | |
| 63 | } | |
| 64 | ||
| 65 | int sizeStr = getSizeInput(); | |
| 66 | - | } |
| 66 | + | System.out.println("The size of the bouncing ball will be " + sizeStr + ".\n");
|
| 67 | ||
| 68 | s = sizeStr; | |
| 69 | } catch (Exception e) {
| |
| 70 | System.exit(0); | |
| 71 | } | |
| 72 | } | |
| 73 | } |