Please read the following function:
public static int getValue(int i) {
int result = 0;
switch (i) {
case 1:
result = result + i;
case 2:
result = result + i * 2;
case 3:
result = result + i * 3;
}
return result;
}
When i = 2, what is the output? I think it is 4, but the answer is 10 by execution. Why does this happen? Why does case 3 also execute? i is 2, not 3. I'm trying to think about it but I still don't know why this happens.
Switch statements require break; for each case to avoid executing later case statements.
Use break to avoid next case execution.
public static int getValue(int i) {
int result = 0;
switch (i) {
case 1:
result = result + i;
break;
case 2:
result = result + i * 2;
break;
case 3:
result = result + i * 3;
break;
}
default:
break;
return result;
}
You are forgetting to `break; the switch statement.
It should look something like this when you are done:
case 1:
//code
break;
case 2:
//code
break;
Also it's always good to include a defualt case:
default:
System.err.println("Something went terribly wrong!");
Sorry if this has been posted before, this should fix everything.
I also have a reason as to why whats happening is happening:
result is being saved from case 1, and reused with the new value. This is getting that funky error of yours.
The case statement needs a break statement to stop it going into the next case statement.
Also good to have a default case
switch (i) {
case 1:
result = result + i;
break;
case 2:
result = result + i * 2;
break;
case 3:
result = result + i * 3;
break;
default:
log.error (...);
}
Use "break;" to prevent the code from running into the next case automatically.
switch (choice)
{
case 0:
//codes
break;
case 1:
//codes
break;
case 2:
//codes
break;
case 3:
//codes
break;
}
Related
So i am doing a simple menu in main class where it has 10 options from 0 to 9, i am using a switch case to get the option and then execute a certain code, and number 9 is to do all the options in the menu.
How can that if option is 9, it does me all the cases before.
public static void main(String[] args) {
switch (option) {
case 1:
do A;
break;
case 2:
do B;
break;
case 3:
do C;
break;
case 4:
do C;
break;
case 5:
do C;
break;
case 6:
do C;
break;
case 9:
do case 1 ;
do case 2 ;
do case 3 ;
do case 4 ;
do case 5 ;
do case 6 ;
}
}
I am expecting that when option is 9, it executes all the cases before;
There are various ways to achieve what you need. I'll outline a few.
Use methods
This is simple, and somewhat clean and easy to maintain. All you need to do is wrap the code you would like to execute in each case in a separate method then call those methods from within the switch. This is exactly what functions are for.
switch(option){
case 1:
doA();
break;
case 2:
doB();
break;
...
// other cases
...
case 9:
doA();
doB();
...
// other method calls
...
break;
}
Switch to if statements
This is pretty self explanatory, just check if the option is each different case or option 9.
if(option == 1 || option == 9){
do A;
}
if(option == 2 || option == 9){
do B;
}
...
// other cases
...
(Mis)use breaks
This is fairly ugly and I wouldn't recommend it but it's really up to personal preference (and how easy to read and maintain you want the code to be in the future).
If option is 9, then we flip a flag to turn off all breaks in the switch statement. This effectively makes all other cases below it just execute linearly (as the breaks to leave the switch are disabled).
boolean isCase9 = false;
switch(option){
case 9:
isCase9 = true;
case 1:
doA();
if(!isCase9){
break;
}
case 2:
doB();
if(!isCase9){
break;
}
...
// other cases
...
I suggest defining a method (let's call it switchMethod) with the switch case - when all options are only 0 to 8.
public void switchMethod(option) {
switch (option) {
case 1:
do A;
break;
case 2:
do B;
break;
case 3:
do C;
break;
....
case 8:
do X;
break;
}
}
In the main:
if (option < 9) {
switchMethod(option)
} else {
for (int i = 0; i <9; i++)
switchMethod(i)
}
That way you won't write duplicate code and it will do the logic you requested.
Another way is doing duplicate code like that:
switch (option) {
case 1:
do A;
break;
case 2:
do B;
break;
case 3:
do C;
break;
case 4:
do C;
break;
case 5:
do C;
break;
case 6:
do C;
break;
case 9:
do A;
do B;
do C;
....
break;
}
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.
I'm trying to make a switch statement that prints out different messages based on whether the user inputs a 1, 2, or 3 but I keep getting errors. Just the small snippet of code below throws about a dozen errors which is probably some sort of record for me. :/
int menuSelection = keyboard.nextint;
switch
case 1: int menuSelection = "1";
break;
case 2: int menuSelection = "2";
break;
case 3: int menuSelection = "3";
break;
The error message is
"case, default, or '>' expected"
but I don't know what that means or how to fix it.
The syntax for your switch statement is incorrect. You need to switch on the variable and also add brackets to enclose your cases:
switch (menuSelection) {
case 1:
break;
case 2:
break;
case 3:
break;
}
Based on the below observations, the switch statement may look like this:
int menuSelection = keyboard.nextint;
String menuSelectionDisplay;
switch(menuSelection)
{
case 1:
menuSelectionDisplay = "1";
break;
case 2:
menuSelectionDisplay = "2";
break;
case 3:
menuSelectionDisplay = "3";
break;
default:
menuSelectionDisplay = "?";
break;
}
Switch statements are required to be surrounded by curly braces {}
It's considered a good idea to have a "default" clause within your switch statement. It acts as a catch-all that gets executed when none of the cases match. In the above example, if the value of menuSelection happened to be 4, the default clause would be executed.
The switch statement takes in one parameter, which is used to determine which case to run. Think of it as a series of if-else statements.
int value = 5;
switch(value)
{
case 5: System.out.println("Hello!"); break;
case 6: System.out.println("Bye!"); break;
default: System.out.println("Huh?"); break;
}
is equivalent to this:
if(value === 5)
{
System.out.println("Hello!");
}
else if(value === 6)
{
System.out.println("Bye!");
}
else
{
System.out.println("Huh?");
}
In your cases, you appear to be setting the value of an int to a String. That's invalid; you would need to either
Set the value of a String to be a String (String s = "1";), or
Set the value of an int to be an int (int i = 1;).
Finally, you cannot re-declare a variable's type multiple times within the code (that's having int menuSelection = keyboard.nextint; and int menuSelection = 3; in the same scope)
int menuSelection = keyboard.nextint;
switch (menuSelection) {
case 1:
menuSelection = "1";
break;
case 2:
menuSelection = "2";
break;
case 3:
menuSelection = "3";
break;
default:
break;
}
<%
String amount2="0";
amount2=request.getParameter("amount");
// out.println(amount2);
// Double amount1=0.00;
Double amount1=Double.parseDouble(amount2);
out.println("\n Double amount is = "+amount1);
Double result=0.0;
String currency=request.getParameter("source");
int ch=Integer.parseInt(currency);
out.println("\n selection value is "+ch);
switch(ch)
{
case 1 :
{
out.println("In GBP");
result=amount1*100.70;
out.println(result);
}
case 2 :
{
result=amount1*0.59;
}
case 3 :
{
result=amount1*0.043;
}
case 4 :
{
result=amount1*56.64;
}
case 5 :
{
result=amount1*54.91;
}
case 6:
{
result=amount1*60.17;
}
case 7:
{
result=amount1*52.15;
}
}
out.println(result);
%>
When i Try to print the value of the result variable it shows two different results.The value printed inside the switch case is correct but when i print it outside switch then it changes. i want to know why it is happening. Is it a problem with my IDE or something else ? HElP !!
You're missing break; after each of your cases.
case 1:
{
out.println("In GBP");
result=amount1*100.70;
out.println(result);
break;
}
case 2:
{
result=amount1*0.59;
break;
}
case 3:
{
result=amount1*0.043;
break;
}
case 4:
{
result=amount1*56.64;
break;
}
case 5:
{
result=amount1*54.91;
break;
}
case 6:
{
result=amount1*60.17;
break;
}
case 7:
{
result=amount1*52.15;
break;
}
Otherwise program flow will continue and all cases below the matching case will be executed.
Here I have some problem when I use while loop in switch statement using dialog boxes. Some statements are unreachable and dialog boxes not appeared. Please help me! And also can do some correction on my code.
This the simple code that I made:
public static void main(String[] args)
{
// prompt and read first number from user
String no = JOptionPane.showInputDialog(null, "Enter the number");
int num = Integer.parseInt(no); //convert string to number
switch (num)
{
//display result
default: JOptionPane.showMessageDialog(null,"fail"); break;
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
}
}
The cases in a switch/case are evaluated in the order you put them. default matches all cases. Since you have that first and that case does something before breaking out of it, the other cases will never be reached. Try this instead:
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); break;
Your code does not show a while loop anywhere. Perhaps you can update with the code you attempted.
switch (num)
{
case 1:
while(!your condition)
{
JOptionPane.showMessageDialog(null,"c=a+b");
}
break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); break;
}
Retype the code:
// prompt and read first number from user
String no = JOptionPane.showInputDialog(null, "Enter the number");
int num = Integer.parseInt(no); //convert string to number
while (num<=4)
{
if
switch (num)
{
//display result
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); continue;
}
}// end method main
}// end class abc