#If-else confusion (beginner)

12 messages · Page 1 of 1 (latest)

knotty mirage
#

So I got into learning Java the other day and was doing my basic project from A + B sum over to reversing strings and doing basic calculators, and I used mostly "if-else" for multiple-choice stuff like my random number guessing game, like the following:

```java
while (!validRange){
        rInput = scanner.nextInt();
        if (rInput == 1){
            validRange = true;
            System.out.println("Please Guess a Number between: 0 - 10");
        }else if(rInput == 2){
            validRange = true;
            System.out.println("Please Guess a Number between: 0 - 20");
            rNumberMax = 20;
        } else if (rInput == 3) {
            validRange = true;
            System.out.println("Please Guess a Number between: 0 - 30");
            rNumberMax = 30;
        } else {
            System.out.println("Invalid Input. Please enter 1,2 or 3!");
        }

    }```

Now that I want to do a Contact Book/Address book as my next project, this "if-else" brick wall becomes longer and longer, and I feel like I'm overlooking an easy step on how I can get around this. I know I can use a switch statement or change the loop to this:

while (!validRange){
    rInput = scanner.nextInt();
    if (rInput == 1 || rInput == 2 || rInput == 3){
        validRange = true;
        if (rInput == 1){
            System.out.println("Please Guess a Number between: 0 - 10");
        } else if (rInput == 2){
            System.out.println("Please Guess a Number between: 0 - 20");
            rNumberMax = 20;
        } else {
            System.out.println("Please Guess a Number between: 0 - 30");
            rNumberMax = 30;
        }
    } else {
        System.out.println("Invalid Input. Please enter 1, 2, or 3!");
    }
}```

Which would make it a bit shorter; however, I feel like I'm still missing something crucial since this doesn't seem like good code. Any tips/help is appreciated. Much thanks.
fiery wrenBOT
#

This post has been reserved for your question.

Hey @knotty mirage! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pseudo crescent
#

A switch is easily the most readable form to represent user choice. Of course you could do something complicated to avoid having to repeat the same form of print statements but that would be way too much effort. To keep the code clean and readable use a switch.

knotty mirage
#

I was told that the thing I did in the bottom text on the thread wouldnt work in a switch statement tho since I would need to do it like this then

switch (rInput) {
    case 1:
    case 2:
    case 3:
        validRange = true;
        switch (rInput) {
            case 1:
                System.out.println("Please Guess a Number between: 0 - 10");
                break;
            case 2:
                System.out.println("Please Guess a Number between: 0 - 20");
                rNumberMax = 20;
                break;
            case 3:
                System.out.println("Please Guess a Number between: 0 - 30");
                rNumberMax = 30;
                break;
        }
        break;
    default:
        System.out.println("Invalid Input. Please enter 1, 2, or 3!");
}```
or is there another way to make sure that my boolean gets switched to True if any of the cases are used?
pseudo crescent
#

There is no need for a nested switch here. To check whether or not the user has entered a valid value you could also use the rNumberMax variable itself and set it to something invalid (like -1) when the user choice isnt valid.
Your code boils down to:

#
while(rValue == -1) {
switch(rInput) {
 ...
}
if(rInput == -1){
 //Invalid
} else {
 //Valid
}
}
fiery wrenBOT
#
while(rValue == -1) {
	switch(rInput) {
		...
	}
	if(rInput == -1){
		//Invalid
	} else {
		//Valid
	}
}

knotty mirage
#

oh I see

#

makes perfect sense actually I think I was just to focused on getting my terrible code to work. Thanks for the help

fiery wrenBOT
pseudo crescent
#

You could also use a switch expression for the first switch. I inferred the use of a traditional switch