Home > Back-end >  Why illegal input, the program or continue to execute the following code?
Why illegal input, the program or continue to execute the following code?

Time:04-16

Consult everybody a great god, why must be set in the setter age between 0-100, but even if input the wrong number, program or have been executed exactly?


The class getter {
private String name;
Private int the age;

Public String getName () {
return this.name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public int getAge () {
Return this. The age;
}

Public void setAge (int age) {
If (age> 0 & amp; & Age<100) {
this.age=age;
} else {
System. Out.println (" illegal input, please input again!" );
return;
}
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
import java.util.Scanner;

Public class test {
Public static void main (String [] args) {
Getter g=new getter ();
Scanner sc=new Scanner(System.in);
System. The out. Println (" please input your name: ");
String s1=sc. NextLine ();
G.s etName (s1);
System. The out. Println (" please enter the age: ");
Int s2=sc. NextInt ();
G.s etAge (s2);
System. The out. Println (g.g etName ());
System. The out. Println (g.g etAge ());
System. The out. Println (" my name is "+ g.g etName () +", "+" I am "+ g.g etAge () +", ");
}
}

CodePudding user response:

Just print the words, and not interrupt program,

CodePudding user response:

Return just over current method, namely setAge method, main continues execution

CodePudding user response:

If your ide intelligent enough, it will prompt you to 'return' unnecessary, because is the last statement of "void" method

CodePudding user response:

Ide does prompt return unnecessary, if the input error need to terminate the run and wait for the input again, how should do? Thank you very much!
  • Related