java - Having trouble with looping a Switch case and nested while loop -
i quite new programming first semester in college , no prior knowledge. working in java after working in python , working on fortune telling program. main issue i'm having trying homecoming switch inquire if user play 1 time again or if entered invalid response that's outside of 8 cases. must have while loop nested within while loop.
scanner user = new scanner(system.in); system.out.println("welcome fortune telling program.\n"); //welcome message system.out.print("would me tell fortune? type 1 yes , other number no: "); //ask yes or no run int var0 = user.nextint(); if (var0 == 1) { system.out.print("enter number 1-8 , tell fortune: "); //ask number between 1-8 find fortune or invalid int var1 = user.nextint(); switch (var1) { case 1: //case 1-8 fortunes system.out.println("\nyou become great if believe in yourself."); break; case 2: system.out.println("\nserious problem bypass you."); break; case 3: system.out.println("\nyou travel many exotic places in lifetime."); break; case 4: system.out.println("\nyour ability accomplishment follow success."); break; case 5: system.out.println("\nwhen fear hurts you, conquer , defeat it!"); break; case 6: system.out.println("\nyou called in fulfill position of higher honor , responsibility."); break; case 7: system.out.println("\nyour golden chance coming shortly."); break; case 8: system.out.println("\nintegrity doing right thing, when nobody watching."); break; default: system.out.print("that's not valid number. seek again.\n"); //invalid number seek rerun right response } } //display next print on case not default system.out.print("would fortune? type 1 yes , other number no: "); //loop 'switch' int var2= user.nextint(); system.out.print("thank trying fortune telling program."); //thank message user.close(); } }
a bit of conceptual help:
when "i need homecoming switch..." means need loop.
what parts need repeating? behavior (or rather, instructor) expecting after telling fortune, question "would me tell fortune" appear again. means have set it, , entails (the fortune telling itself) in loop.
the usual build in case is
display question get user input loop, status user did not come in "finish" input do whatever task user input requires. display question again get user input again, next time loop checks status have fresh value calculation.can think of parts of programme fit pattern?
now next thing need while within while. here hint: programme expects user come in values 1-8. if entered '9' or '0' or whatever, should programme ignore , inquire him 1 time again if wants fortune told, or should insist?
java while-loop switch-statement
No comments:
Post a Comment