So I have this code,
public class testestest {
public static void main (String[]args) {
int b=2;
tst tst = new tst();
if (tst.tst(b)==1) {
System.out.println("a=1");
}
else if (tst.tst(b)==2) {
System.out.println("a=2");
}
else if (tst.tst(b)==3) {
System.out.println("a=3");
}
}
}
class tst {
int tst (int b) {
if (b==2) {
System.out.println("print2");
return b;
}
if (b==3) {
System.out.println("print3");
return b;
}
return 0;
}
}
when you execute it it prints print2 print2 a=2. If I change int b to 3, then it prints print3 print3 print3 a=3. I am not sure as to why tst.tst(b) method loops if statement in tst class for the number of b.
Can somebody explain to me why it's doing this? Also, how could I change my code so that it doesn't loop, but rather execute the method only once?
Aucun commentaire:
Enregistrer un commentaire