java - 不断收到未声明的变量错误,我不明白从哪里来

标签 java compiler-errors initialization

My code

我不断收到错误

main.java:77: error: variable fAnswer might not have been initialized
out.println("The answer is: " + fAnswer);
                                ^

我找不到原因。

最佳答案

该错误表明,根据 choice 值,fAnswer 可能无法初始化。例如,如果您有一个 choice == 4,则在调用 out.println("The answer is: "之前,fAnswer 不会为其分配值+ f答案)。这意味着 fAnswer 将指向垃圾,它可能不是您想要的。

尝试在声明时定义它

double fAnswer = -1.0; // Or something like this.

关于java - 不断收到未声明的变量错误,我不明白从哪里来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26570024/

相关文章:

c# - 如何在 xUnit 中执行全局设置/拆卸并并行运行测试?

c++ - 如何在运行时设置常量配置参数

java - org.hibernate.LazyInitializationException : could not initialize proxy - no Session, encore un fois

java - 优化文件下载

c++ - 类声明错误: insufficient contextual information to determine type

java - 为什么我在 Eclipse 中出现警告以及如何在最后导入时删除它 : The import java. sql.SQLException 从未使用过

Java,将 RandomAcessFile 移动到一行的开头

java - 无法在 hibernate 中提取 ResultSet

c - 错误: Expected expression before 'DATA/* : typedef struct DATA DATA */

c - C 程序中的段错误(核心转储)