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 String 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 it
- if (colorStr.equals("red") || colorStr.equals("blue")) {
- return colorStr;
- }
- // 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();
- }
- public static int getSizeInput() throws Exception{
- // give the user two tries
- for (int i=0; i < 2; i++) {
- System.out.println("Enter the size of the ball (3 to 200):");
- int sizeStr = CONSOLE.nextInt();
- // If they input a valid input, return it
- if (sizeStr >= 3 && sizeStr <= 200) {
- return sizeStr;
- }
- // If we haven't returned yet, their choice was invalid.
- System.out.println(sizeStr + " 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();
- }
- public static void mEthod() {
- Color c = null;
- int s = 0;
- try {
- //read in the color
- String colorStr = getColorInput();
- System.out.println("The bouncing ball will be " + colorStr + ".");
- // Check which color they entered, and set c to the corresponding value
- if (colorStr.equals("red")){
- c = Color.RED;
- }else {
- c = Color.BLUE;
- }
- //read in the size
- int sizeStr = getSizeInput();
- System.out.println("The size of the bouncing ball will be " + sizeStr + ".\n");
- s = sizeStr;
- } catch (Exception e) {
- System.out.println("Exiting..");
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement