Java question: is it possible to have a switch statement in another?

I have a question or answer yes or no. I would like to ask another question, yes or no, if the answer was yes. My instructor wants us to use charAt (0) as input for the response.

Is it possible to have a switch statement inside another (as a nested if statement)?

EDIT: Here is an example of my pseudocode =

display "Would you like to add a link (y = yes or n = no)? " input addLink switch (link) case 'y': display "Would you like to pay 3 months in advance " + "(y = yes or n = no)?" input advancePay switch(advPay) case 'y': linkCost = 0.10 * (3 * 14.95) case 'n' linkCost = 14.95 case 'n' linkCost = 0.00 
+7
java switch-statement
source share
4 answers

Yes, but no. If you need the next level of logic, put the second switch in your own method with a descriptive name.

EDIT: In the example you added, you have two Boolean conditions. I would recommend not using the switch at all, using if and else conditions instead. Use (STRING.charAt(0) == 'y') as a test case or something like the boolean isY(final String STRING) { return (STRING.charAt(0) == 'y' || STRING.charAt(0) == 'Y'); } method boolean isY(final String STRING) { return (STRING.charAt(0) == 'y' || STRING.charAt(0) == 'Y'); } boolean isY(final String STRING) { return (STRING.charAt(0) == 'y' || STRING.charAt(0) == 'Y'); }

+18
source share

Yes. Switches break the language block operator pattern, but this is mainly because of the C / C ++ that the switch statement used by Java is based on.

In all three languages, the switch statement takes the following form:

 switch(variable) { case n: statement 1; statement n; (optional) break; case n+1: statement 1; statement n; (optional) break; // Optionally more statements (optional) default: statement 1; statement n; } 

Since the switch statement violates the traditional language pattern, many programmers transfer their multiple statements to the case using the traditional block style: {}

This is due to the fact that most constructions in all three languages ​​allow us to consider block style statements as a single statement, but the switch statement does not require that the block style execute several statements in one case.

Without the break statement separating each case, it will β€œfail” - if case n was agreed and had no gap, the code below it (case n + 1) would be executed - if case n + 1 was not broken and matched, the code by default it will be executed if none of them had a break, if case n matches, the code for case n, case n + 1 and default will be executed.

The default value is optional and indicates the default action for the switch statement to execute. Typically, the default condition is either a general handler or a good place to throw an exception if the value cannot be logically different than the values ​​in the switch statement.

To illustrate the switch statement that runs inside the switch statement, look at this contrived example:

 String message = null; int outerVariable = getOuterVariable(); switch(outerVariable) { case n: statement 1; statement n; break; case n+1: int innerVariable = getInnerVariable(); switch(innerVariable) { case 1: message = "IT WAS 1"; break; default: message = "WHY WOULD YOU DO THIS? OH THE HUMANITY!"; } break; // Optionally more statements (optional) default: statement 1; statement n; } 
+2
source share

Perhaps, but it would be very difficult and difficult to read. There is almost certainly a better way, for example, calling a method in the first switch statement and performing any necessary processing (including, if necessary, another switch) in this method.

+1
source share

Most programming languages ​​are orthogonal. This means that the use of a function is usually location independent if it makes sense.

For example, you cannot declare a local variable public.

0
source share

All Articles