+ 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??
4 Antworten
+ 29
You can use label like this : https://www.tutorialspoint.com/javaexamples/method_label.htm
+ 7
Thanks @Shamima and @Edward, I kinda didn't understand the label way though, gotta search it :-)
+ 7
Got it!! nice one @Shamima
+ 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");
}