Java 丢失返回错误
} 我正在尝试使用户不能插入0或负数。然而,我认为我的逻辑不正确,因为我得到了一个丢失的返回错误。有人能帮忙吗?Java 丢失返回错误,java,methods,return,Java,Methods,Return,} 我正在尝试使用户不能插入0或负数。然而,我认为我的逻辑不正确,因为我得到了一个丢失的返回错误。有人能帮忙吗? 谢谢 方法的所有路径都应该返回某些内容,因为您将其定义为 /** * Read a positive integer and return its value * @param the prompt to be shown to the user */ public static int readPositiveInteger(String prompt) { Syst
谢谢 方法的所有路径都应该返回某些内容,因为您将其定义为
/**
* 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();
}
}
您的方法签名如下:
public static int readPositiveInteger(String prompt)
但是,您永远不会返回整数。你有两个选择:
- 添加类似于
返回0的内容代码>到方法的末尾
- 将
更改为static int
static void
0
在那里
不过,最好的选择是修改
if
语句以使用while
循环,并且仅当用户输入了某个内容时才返回该内容。看起来您希望代码处于while循环还是什么?
就目前情况而言,它可以“从底部跑出去”——正如错误所说,没有返回(这是必需的)
返回类型为int,因此您应该在函数中返回整数值。您需要一个while循环。在伪代码中:
public static int readPositiveInteger()
{
return int_type;
}
if
应为,而:
While true (ie loop forever)
Ask for input
If input ok return it
实际上,没有必要保持为正
布尔值,因为从它经过的那一刻起
while (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块中添加return语句,否则代码无法编译
您的方法需要在else块中使用return语句
if (input > 0)
正如其他人在这里提到的,您的外部if语句应该是while循环:
else
{
System.out.println ("Bad input enter an integer.");
positive = false;
scan.nextLine();
return 0;//your missing return statement
}
}
另外,在if语句中只编写了一个return语句。如果简单地将上述If语句转换为while循环没有帮助,请在while循环之后尝试返回0
java编译器(以及其他编译器)不理解代码的语义。它只能检查语法是否正确。因此,在您的情况下,可能会担心永远无法到达return命令。函数末尾的返回0
将解决此问题(尽管返回0
永远无法到达)。您需要做的是添加返回输入代码>位于所选方法的末尾
while (scan.hasNextInt() && positive == false)
{
...
}
此功能将非常适合您
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();
}
return 0;
}
publicstaticint-readPositiveInteger(字符串提示){
System.out.println(提示);
System.out.println(“输入整数”);
扫描仪扫描=新扫描仪(System.in);
布尔正=假;
int输入=0;
while(输入
}如果第一个参数不是正数,则返回0并退出该函数。这可能不是提问者想要的。您必须返回一些内容,如果控制传递到一个返回语句缺失的循环中,您提到的错误将出现,请在需要时应用返回语句
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();
}
return 0;
}
public static int readPositiveInteger(String prompt) {
System.out.println(prompt);
System.out.println("Enter an integer");
Scanner scan = new Scanner(System.in);
boolean positive = false;
int input = 0;
while (input <= 0) {
System.out.println("please enter a number greater then 0");
input = scan.nextInt();
}
return input;
}
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;
int input;
if (scan.hasNextInt() && positive == false)
{
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();
}
return input;