java - 无法解析为 Java 中的变量
问题描述
我在 Java 中遇到了这个问题。当我运行程序时,它给了我错误:variable_name 无法解析为变量。
if(name.equalsIgnoreCase("...")) {
System.out.print("...");
float name1=SIn.readFloat();
} else if(name.equalsIgnoreCase("...")){
System.out.print("...");
float name2=SIn.readFloat();
}
try {
float converted=name1*valueget; //the error is here with the variable 'name'
System.out.println(...);
}
解决方案
答案很简单,您正在尝试使用不存在的变量。我们只是说, first if returns false
, then 变量name1
永远不会初始化。
if(name.equalsIgnoreCase("...")) { // doesn't equal
System.out.print("...");
float name1 = SIn.readFloat(); // not initialized
} else if(name.equalsIgnoreCase("...")){
System.out.print("...");
float name2 = SIn.readFloat();
}
try {
float converted = name1 * valueget; // using non-existing variable
System.out.println(...);
}
您应该做的是,在该语句之前声明一个变量if-else
并在if-else
.
float floatName = 0; // set it up as zero, because you don't have else statement
if(name.equalsIgnoreCase("...")) {
System.out.print("...");
floatName = SIn.readFloat();
} else if(name.equalsIgnoreCase("...")){
System.out.print("...");
floatName = SIn.readFloat();
}
try {
float converted = floatName * valueget;
System.out.println(...);
}
推荐阅读
- hadoop - 添加节点/Hadoop
- azure - 如何在 CosmosDB Javascript 存储过程中执行批量字段重命名
- javascript - 如何使用包含字符“-”的键的 webpack 4 JSON 摇树功能?
- python - 获取 GeoJson 多边形的大小(OpenStreetMap 方式)
- go - Golang 缓冲通道在发送之前接收数据
- node.js - 使用nodejs在mongodb中保存替换的字符串数据
- azure - 将 Azure 资源复制到客户端 Azure 帐户
- spring - Spring Boot 自动配置的指标未到达 Librato
- html - *ngIf 或 [hidden] 无法正常工作
- ios - 传递参数时,ViewController 的变量值由前向 ViewController 更改