I have a switch statement which works fine, but I feel like it's poorly formatted and redundant(many of the cases do the same thing). How can I improve this?
switch (number) {
case 10: // do 1
break;
case 12: // do 2
break;
case 13: // do 1
break;
case 15: // do 3
break;
case 17: // do 2
break;
case 18: // do 2
break;
}
I guess you can group the cases with the same output together. It is one way to improve this.
switch (number) {
case 10:
case 13:
// do 1
break;
case 12:
case 17:
case 18:
// do 2
break;
case 15:
// do 3
break;
}
break; is used to terminate a case in the switch statement. So, the cases will have same output when there is no break; between them.
You could improve it by removing a few break statement, like this
switch (number) {
case 10:
case 13: // do 1
break;
case 12:
case 17:
case 18: // do 2
break;
case 15: // do 3
break;
}
You may make it a bit more compact like this:
switch (number) {
case 10:
case 13:
System.out.println("do 1 + " + number);
break;
case 12:
case 17:
case 18:
System.out.println("do 2 + " + number);
break;
case 15:
System.out.println("do 3 + " + number);
break;
}
And the last break; is not required in your case but good practice anyways for defensive programming.
You can add default case. When provided cases are not equal to number default statement is executed. Code:
int number = 0;
switch (number) {
case 10: // do 1
break;
case 12: // do 2
break;
case 13: // do 1
break;
case 15: // do 3
break;
case 17: // do 2
break;
case 18: // do 2
break;
default:
System.out.println(number);
}
Related
I'm still learning. Today I have a question in SoloLearn, into The switch Statement lesion:
int day = 3;
switch(day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
// Outputs "Wednesday"
and I thinks, I can have a example to remember about input and switch Statement: I want input a number, and 1 as Monday, 2 as Tuesday.... to 7 as Sunday, and repeat: 8 as Monday, 9 as Tuesday....
This is my code:
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner day = new Scanner(System.in);
if (day.nextInt()>7){
day.nextInt()=day.nextInt()%7;
}
switch(day.nextInt()){
case 1 :
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
case 7:
System.out.println("Sunday");
break;
}
}
}
and error. My knowledge's not enough to fix this thing (I think so, because i'm a newbie and still learning). Can you expand for me, fix this thing and thanks for teach!
Have fun!
The problem is here:
if (day.nextInt()>7){
day.nextInt()=day.nextInt()%7;
}
You're trying to assign a value back to the method call day.nextInt(), which you can't do. Instead, try something like the following:
int dayNum = day.nextInt() % 7;
Also, your switch includes a case for dayNum == 7, which will never be the case.
As mentioned by #kolosy You are missing a break statement below case 0 and by writing day.nextInt() four times, you are actually asking the user to enter four times.
Also you cant day.nextInt()=day.nextInt()%7; do this. By doing it you're trying to assign a value back to the method call Change your code of main function to this
Scanner day = new Scanner(System.in);
int enteredDay = day.nextInt();
if (enteredDay > 7) {
enteredDay = enteredDay % 7;
}
switch (enteredDay) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
case 7:
System.out.println("Sunday");
break;
}
Add break after case 0:
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
If the value in switch() goes beyond 7 than you will get error as you have not provided any default case here. You can either do something like number%7to get the value always in case range.
You are missing a break statement below case 0. Java does not allow label fall-through like some other languages.
Also - you should be checking for your number being >= 7, not >7, or you'll never get a 0 on the second week. 8 % 7 is 1
NextInt() is a method .. you can't assign it a value
You need to use nextInt() once to get the input and store it in a variable. Every time you call nextInt() it would look for the next integer value entered.
Scanner day = new Scanner(System.in);
int dayInput = day.nextInt();
if (dayInput>7){
dayInput=dayInput%7;
}
switch(dayInput){
case 1 :
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
case 7:
System.out.println("Sunday");
break;
}
You could solve this by creating an int value to set the input for day as day.nextInt() then you can do the math in the created variable and make the switch depending on that value, this should work:
Scanner day = new Scanner(System.in);
int dayVal = day.nextInt();
if (dayVal>7){
dayVal %= 7;
}
switch(dayVal){
case 1 :
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
}
}
As said before, you need to change 7 to 0 because you will never have a residue of 0, therefore you will never get a Sunday
I am working on a menu program and I want to be able to enter as many selections as I want without the menu looping as well after the input, currently it either infinite-loops, or the program ends after one input, being unable to perform a different selection after the first. Also I want to have a case where I exit the menu if I press 0.
public void showMenu() {
System.out.println("Welcome!");
System.out.println("Select an option:\n" +
"1. Adunare\n" +
"2. Scadere\n" +
"3. Inmultire\n" +
"4. Impartire\n" +
"5. Comparare numere\n" +
"6. List To Hundred\n" +
"7. Nr to list\n" +
"8. Contains\n" +
"9. Even numbers\n" +
"10. List of Strings\n" +
"11. Second largest number\n" +
"12. Second lowest number\n" +
"13. Number \n" +
"14. Number 2 \n" +
"15. Number 3\n" +
"16. String\n" +
"17. String 2\n" +
"18. Amount of snow\n" +
"19. Eligible to vote test\n" +
"20. Odd or even\n" +
"21. Dog\n" +
"22. Cat\n" +
"23. Elev");
}
public void runProgram() {
showMenu();
int numberFromUser = citire.readNumbers();
do {
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Add
numberFromUser = citire.readNumbers();
into the loop to take numbers in the loop.
public void runProgram() {
showMenu();
int numberFromUser;
do {
numberFromUser = citire.readNumbers();
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Try changing runProgram to following :
public void runProgram()
{
int numberFromUser;
showMenu();
while(true)
{
System.out.print("Enter choice: ");
numberFromUser = citire.readNumbers();
switch(NumberFromUser)
{
// All your case statements
case 0: System.exit(0);
}
}
}
This question already has answers here:
Using switch statement with a range of value in each case?
(20 answers)
Closed 6 years ago.
I'm new to java programming and I'm having trouble figuring this out.
Here's my code:
boolean running = true;
PANTS:
while (running) {
int waistMeasure = in.nextInt();
if (waistMeasure >= 26 && waistMeasure < 28){
System.out.println("You are a Small.");
break;
}
if (waistMeasure >= 28 && waistMeasure < 30){
System.out.println("You are a Medium.");
break;
}
if (waistMeasure >= 30 && waistMeasure <= 34){
System.out.println("You are a Large.");
break;
}
else {
System.out.println("Invalid input");
}
}
Is there any way to convert this into switch case statements?
Yes, there is – but you’d have to create a case for each value (as shown below). You cannot express a range of values with a single case. So if you want readable and maintainable code, you should stick with ifs.
switch (waistMeasure) {
case 26:
case 27:
System.out.println("You are a Small.");
running = false;
break;
case 28:
case 29:
// ....
}
switch (waistMeasure) {
case 26:
case 27:
System.out.println("You are a Small.");
break PANTS;
case 28:
case 29:
System.out.println("You are a Medium.");
break PANTS;
case 30:
case 31:
case 32:
case 33:
case 34:
System.out.println("You are a Large.");
break PANTS;
default
System.out.println("Invalid input");
}
Do you mean something like this?
Notice that you have to use the PANTS label for the break statement to exit the loop, otherwise they just break from the switch and will continue looping.
PANTS: for (;;) {
int waistMeasure = in.nextInt();
switch (waistMeasure) {
case 26:
case 27:
System.out.println("You are a Small.");
break PANTS;
case 28:
case 29:
System.out.println("You are a Medium.");
break PANTS;
case 30:
case 31:
case 32:
case 33:
case 34:
System.out.println("You are a Large.");
break PANTS;
default:
System.out.println("Invalid input");
}
}
I am making code for my CPSC class and I have to print up to a case number that is set for the int. When I enter "2", the code prints "two potato" eight times instead of "One potato, two potato".
Here is my code I have:
public class Potato {
public Potato() {
}
public void count(int c) {
for (int i = 0; i < 8; i++) {
switch (c % 8) {
case 1: System.out.println("One potato"); break;
case 2: System.out.println("two potato"); break;
case 3: System.out.println("three potato"); break;
case 4: System.out.println("four..."); break;
case 5: System.out.println("five potato"); break;
case 6: System.out.println("six potato"); break;
case 7: System.out.println("seven potato"); break;
case 8: System.out.println("more!"); break;
default: break;
}
}
}
}
I think my problem is my for-loop, not too sure though, since I am here asking for help. Thanks in advance!
It is because c % 8 (2 % 8 = 2), so in every loop it will execute case 2 and prints two potato. You could use i % 8 instead.
This should do the trick. As others have said, you should use i%8. However, for this to work you should also start with i=1 since 0%8 = 0. And your case 8 should be changed to case 0.
for (int i = 1; i <= c; i++) {
switch (i % 8) {
case 1: System.out.println("One potato"); break;
case 2: System.out.println("two potato"); break;
case 3: System.out.println("three potato"); break;
case 4: System.out.println("four..."); break;
case 5: System.out.println("five potato"); break;
case 6: System.out.println("six potato"); break;
case 7: System.out.println("seven potato"); break;
case 0: System.out.println("more!"); break;
default: break;
}
}
This code is... peculiar:
The main problem is that you are not using the i variable that is incremented at the for loop. Instead, you are using c variable:
switch (c % 8)
Should be:
switch (i % 8)
You don't need the case 8 since there is no way that i % 8 result in 8. And, looks like you don't need c variable at all.
This question already has answers here:
If-else working, switch not [duplicate]
(9 answers)
In a switch statement, why are all the cases being executed?
(8 answers)
Closed 8 years ago.
For the following switch statement:
if a value between 0-9 is selected, output is fine. If value greater than 9 is selected, output is always a lowercase z.
for (int i = 0; i < 3; i++)
{
random[i] = randomnumber.nextInt(36);
if (random[i] > 9)
{
switch(random [i])
{
case 10: character[i] = "A";
case 11: character[i] = "B";
case 12: character[i] = "C";
case 13: character[i] = "D";
case 14: character[i] = "E";
case 15: character[i] = "F";
case 16: character[i] = "G";
case 17: character[i] = "H";
case 18: character[i] = "I";
case 19: character[i] = "J";
case 20: character[i] = "K";
case 21: character[i] = "L";
case 22: character[i] = "M";
case 23: character[i] = "N";
case 24: character[i] = "O";
case 25: character[i] = "P";
case 26: character[i] = "Q";
case 27: character[i] = "R";
case 28: character[i] = "S";
case 29: character[i] = "T";
case 30: character[i] = "U";
case 31: character[i] = "V";
case 32: character[i] = "W";
case 33: character[i] = "X";
case 34: character[i] = "Y";
case 35: character[i] = "Z";
}
}
else
character[i] = Integer.toString(random[i]);
A case statement is a form of standidised goto statement, it goes to the case statement and then continues on as usual. To get the behaviour you desire you need a
break;
at the end of each case
Where is the break of the cases man!
switch(random[i]){
case 10: ....
break;
case 11: ....
break;
//and so on
}
Add break with all case blocks like
case 10:
character[i] = "A";
break;