【发布时间】:2012-11-23 05:35:16
【问题描述】:
/**
* Read a positive integer and return its value
* @param the prompt to be shown to the user
*/
public static int readPositiveInteger(String prompt)
{
System.out.println (prompt);
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer");
boolean positive = false;
if (scan.hasNextInt() && positive == false)
{
int input = scan.nextInt();
if (input > 0)
{
positive = true;
{
return input;
}
}
else
{
System.out.println ("Bad input enter an integer.");
positive = false;
scan.nextLine();
}
}
else
{
System.out.println ("Bad input enter an integer.");
positive = false;
scan.nextLine();
}
}
}
我正在尝试使用户无法插入 0 或负数。但是我不认为我的逻辑是正确的,因为我收到了一个丢失的返回错误。任何人都可以帮忙吗? 谢谢!
【问题讨论】:
-
你必须返回一些东西,如果控制传递到一个循环,其中返回语句丢失你提到的错误将会出现,在需要的地方应用返回语句