How to restart programm in java [SLOVED] | Sololearn: Learn to code for FREE!


How to restart programm in java [SLOVED]

I want to restart my code if user input Ok, how can I do this? (I knows about scanners)

1/15/2020 7:02:57 PM


6 Answers

New Answer


For simple code I suggest using a loop. break out of it when user inputs "Ok" or use System.exit(0)


Only you can get the answer. Because that anwser depends on the code itself. Also the value that need to be changed, to allow the code to repeat its cycle.


HNNX 🐿 1. Where I posted my code isn't a java playground so Case being in pascalcase or lowercase does not matter, since I'm aware of that and I'm not writing the actual code for the OP. 2. Everyone has different approach in solving a task and I would have solved mine that way. 3. The OP wants to restart his program , so he prolly needs and input to determine or get the command to restart which takes the actual argument in the switch case, else how would the program knows when to restart ?


base code should be in a method and you can run that method using an infinite while loop along with a switch statement, ( make sure there's a goto keyword also) outer:{ while(true){ switch(someCondition){ Case "exit": break outer; Case "restart": RunCode(); default: RunCode(); } } }


HNNX 🐿 I'm not asking of your opinion, that's it. Everyone is entitle to his/her opinion, if you don't think switch is useful then keep that to yourself as long as I think otherwise. "Switch is use for input " how about int num = (int)(Math.random()*50) switch(num%2){ case 0: System.out.print("even") default : System.out.print("odd") } Is that also an input? Respect my opinion as I respect yours, I don't have time for pointless argument, else you're going into my blocklist


Mirielle🐶 case should be lowercase switch is used for input rather than condition so it may be a bit misleading third, I don't think a method is needed - a block of code can be executed using while(true)