+ 7

GOTO FOR JAVA???

I have made a small program on elipse.Heres a part of it:- int x=input();//input is a method that uses scanner to get input as int. if(x==1) s.o.p("a");//s.o.p=System.out.print else if (x==0) s.o.p("b"); else { s.o.p("Please enter either 0 or 1); *Here I want "something" so as to go back to first line(int x=input()) ,so as to 'loop' again through if -else .Something known as goto in another language(I dont remember the name ,I think its C++) Any way in which I can do it? Alternative for goto in java??

22nd Apr 2017, 4:41 PM
Meharban Singh
Meharban Singh - avatar
4 Antworten
22nd Apr 2017, 4:45 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 7
Thanks @Shamima and @Edward, I kinda didn't understand the label way though, gotta search it :-)
22nd Apr 2017, 8:10 PM
Meharban Singh
Meharban Singh - avatar
+ 7
Got it!! nice one @Shamima
23rd Apr 2017, 4:10 AM
Meharban Singh
Meharban Singh - avatar
+ 5
int x = input (); while (x != 0 && x != 1) { s.o.p("please enter 1 or 0); x = input (); } if (x == 1) { s.o.p("a") } else ( x == 0) { s.o.p ("b"); }
22nd Apr 2017, 4:48 PM
Edward