Compiling Error: Missing return statement(编译错误:缺少返回语句)
问题描述
这个程序用 3 种不同的方法玩掷骰子.我在玩掷骰子时需要帮助,但我需要使用这 3 种不同的方法,但由于某种原因,每次编译时都会出现此错误:
This program plays craps with 3 different methods. I need help in playing craps but i'm required to have these 3 different methods but for some reason every time I compile I am getting this error:
CrapsAnalysis.java:48: error: missing return statement
}
^
1 error
Process javac exited with code 1
代码:
public class CrapsAnalysis
{
public static int rollDie( int n) {
return (int)(Math.random()*n) + 1 ;
}
public static int rollDice( ) {
return rollDie(6) + rollDie(6) ;
}
public static boolean playOneGame( ) {
int newDice = rollDice();
int roll = rollDice(); //first roll of the dice
int playerPoint = 0; //player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;
else if (rollDice() == playerPoint)
return true;
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7) ;
}
}
推荐答案
Java 必须查看所有执行路径.如果 while
循环结束而不返回任何内容会发生什么?您可能在逻辑上阻止了这一点,但 Java 编译器不会进行这种分析.
Java must look at all execution paths. What happens if the while
loop ends without returning anything? You may be logically preventing that, but the Java compiler won't do that analysis.
在 while
循环结束后提供 return
语句,或抛出某种 Exception
(IllegalStateException代码>?)如果代码真的不应该在那里.
Provide a return
statement after the end of the while
loop, or throw some kind of an Exception
(IllegalStateException
?) if the code really shouldn't ever make it there.
这篇关于编译错误:缺少返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:编译错误:缺少返回语句


- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01