0

How i can add while loop here?

import java.util.*; public class Switch { public static void main(String[] args) { int x, y, result , choice; Scanner input = new Scanner (System.in); System.out.print("Enter First Number:"); x = input.nextInt(); System.out.print("Enter Second Number:"); y = input.nextInt(); System.out.println("Choose an opration"); System.out.println("1-addition"); System.out.println("2-substraction"); System.out.println("3-multiplication"); System.out.println("4-divition"); System.out.println("5-exit"); System.out.println("enter your choice"); choice= input.nextInt(); switch (choice) { case 1: result = x + y; System.out.format("Addition is:\n" +result); break; case 2: result = x - y; System.out.format("substraction is:\n"+ result); break; case 3: result = x * y; System.out.format("multiplication is:\n"+result); break; case 4: result = x / y; System.out.format("division is:\n"+result); break; case 5: break; default: System.out.println("choose only a number from 1-5"); break; } } }

20th Nov 2019, 7:23 PM
Roy
Roy - avatar
5 Réponses
+ 10
Roy Hello,😊 Please, as Denise Roßberg suggested👍 you can post the code you're struggling with and include relevant tags! • https://www.sololearn.com/post/75089/?ref=app Also, check out these tips to maximize your chance of receiving useful answers. 😉 • https://www.sololearn.com/Discuss/333866/?ref=app Happy SoloLearning! 🍻
20th Nov 2019, 11:05 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 1
I am not sure what you want to do but I guess you want to run the loop until the user enters 5. int x; int y; int choice; while(choice != 5){ x = input.nextInt(); y = input.nextInt(); choice = input.nextInt(); //your switch statement } Important: Sololearn is not good for interactive codes, because you have to enter all values at the beginning. Next time you should not copy the code inside your question. Post only the link. Your code is in java. So you should use only java as tag. Try to specify your problem. If you want to add a while loop than you should also explain what you want to do.
20th Nov 2019, 10:39 PM
Denise Roßberg
Denise Roßberg - avatar
0
Roy Insert an infinite loop below y=input.nextInt() statement and In case 5 use return or exit (To terminate program..Coz i don't know if java keywords are similar to c++) statement instead of break..This will work the way you want and Take care of terminating statement in case 5
21st Nov 2019, 9:30 AM
Alaska
Alaska - avatar
0
Denise Roßberg your answer is true thank you do you know how I can add (method) for this code?
26th Nov 2019, 3:21 PM
Roy
Roy - avatar
0
Roy public static void printResult (int choice, int x, int y){ //here you can put your switch statement } In your main method you ask the user for int choice, int x and int y. Then you can call the method: printResult (choice, x, y);
26th Nov 2019, 3:48 PM
Denise Roßberg
Denise Roßberg - avatar