您好我是编程新手,所以如果答案非常明显,我很抱歉,但是我的代码无法在两次输入错误后终止,尽管使用了return语句。其余的代码在打印范围和组织和分类之间的3个整数时工作正常。
System.out.println("Please input a interger between 0-99:");
int input1 = Input.nextInt();
if (input1>99||input1<0){
System.out.println("Outside range. Please enter an integer between 0-99");
input1 = Input.nextInt();
}
else if (input1>99||input1<0){
System.out.println("Outside range program terminated.");
return;
}
System.out.println("Please input the second interger between 0-99:");
int input2= Input.nextInt();
if (input2>99||input2<0){
System.out.println("Outside range. Please enter an integer between 0-99");
input2 = Input.nextInt();
}
else if (input2>99||input2<0){
System.out.println("Outside range program terminated.");
System.exit(0);
}
if (input1>input2){
max=input1;
min=input2;
}
else {
min=input1;
max=input2;
}
int integer1 = min + (int)(Math.random()* (( max-min)+1));
int integer2 = min + (int)(Math.random()* (( max-min)+1));
int integer3 = min + (int)(Math.random()* (( max-min)+1));
if(integer1>integer2) {
temp = integer1;
integer1 = integer2;
integer2= temp;
}
else if (integer2>integer3) {
temp = integer2;
integer2=integer3;
integer3=temp;
}
if(integer2>integer3){
temp=integer2;
integer2=integer3;
integer3=temp;
}
System.out.println("The range begins at"+min+"and ends at"+max);
System.out.println("Three sorted random integers between 20 and 80 are:");
if (integer1 %2 ==0) {
System.out.println(+integer1+"\tEven");}
else {
System.out.println(+integer1+"\tOdd");
}
if (integer2 %2 ==0) {
System.out.println(+integer2+"\tEven");
}
else{
System.out.println(+integer2+"\tOdd");
if (integer3 %2 ==0){
System.out.println(+integer3+"\tEven");}
else{
System.out.println(+integer3+"\tOdd");
}
答案 0 :(得分:0)
您的return
语句永远不会执行。
if (input1>99||input1<0){
System.out.println("Outside range. Please enter an integer between 0-99");
input1 = Input.nextInt();
}
else if (input1>99||input1<0){
System.out.println("Outside range program terminated.");
return;
}
您期望return
如何运行?布尔条件相同。如果input1>99||input1<0
则执行第一个if
语句。如果input1>99||input1<0
不成立,那么if
语句都不会执行。在任何一种情况下,都永远不会达到return
。