我有一个调用方法 1 的 main 调用方法 2:
public class azza_wahada_A3Q1 {
public static void main (String[] args) {
Method1 m1 = new Method1();
int age = m1.getValidNumber("Please enter your age.", 0, 110);
//int age = m2.getInteger("Please enter your age.");
System.out.println("u r age is \n"+age);
}
}
public class Method1 {
public static int getValidNumber(String prompt, int min, int max){
int input;
Method2 m2 = new Method2();
Method3 m3 = new Method3();
Boolean range = false;
while(range){
input = m2.getInteger(prompt);
if (input > min && input < max){
range = true;
// return input;
}
else{
m3.showError(input,min, max);
range = false;
}
}
return input;
}
}
import javax.swing.JOptionPane;
public class Method2 {
public static int getInteger(String prompt){
String message;
int getInt;
message = JOptionPane.showInputDialog(null, prompt);
getInt = Integer.parseInt(message);
return getInt ;
}
}
import javax.swing.JOptionPane;
public class Method3 {
public static void showError(int number, int min, int max){
String error_message;
error_message = JOptionPane.showInputDialog(null, "Please enter a new number");
}
}
为什么会这样?代码在没有 while 循环的情况下工作正常,当我引入循环时,我收到错误消息,说我的输入变量可能尚未初始化,在方法 1 中的返回输入处显示错误。这是怎么回事?谢谢