java - 我的java代码中的未知语法
问题描述
我在while 循环结束之前index.hasNext
和input.nextInt();
之前收到语法错误。“input.nextInt();” 对于“权重[索引] = input.nextInt();” 适用,但不适用于“类型 [索引]”。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Horse {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =null;
int[] weights = new int[100];
String[] types = new String[100];
try {
input = new Scanner(new File("horseData.csv"));
} catch (FileNotFoundException e) {
System.out.println(" Error FIle Not FOund");
e.printStackTrace();
System.exit(1);
}//end catch
int index = 0;
while (index.hasNext()) {
weights[index] = input.nextInt();
types[index] = input.nextInt();
System.out.println(" Weight: "+ weights[index] + ", types:" + types[index]);
}//end while
input.close();
for(int i = 0; i < types.length;i++)
System.out.println((i+1) + "\t" + amountOfFeed(types[i], weights[i]));
}//end main
public static double amountOfFeed(String horseType, int horseWeight) {
if(horseType.equals(" maintance") || horseType.equals(" light work") )
return horseWeight * .02;
else if (horseType.equals(" Moderate work"))
return horseWeight * .025;
return horseWeight * .03;
}
}
谢谢:)
解决方案
更改以下内容
while (index.hasNext())
至
while (input.hasNext())
您可能还需要index
使用index++
推荐阅读
- .net - 为什么 System.Text.Json.Serialization.JsonSerializer 编码双引号而不是仅仅转义它们?
- java - 无法添加子实体 - JPA OneToMany 关系
- c# - OPC-UA“值”和“属性值”有什么区别
- vue.js - 安装 VueJS CLI 时如何修复 npm WARN deprecated 错误
- python - 拆分字符串行代码而不拆分其输出
- asp.net-core - 如何在.net核心中为多个请求运行api
- c++ - 为什么我不能将 rand() 与数组的大小一起使用?
- html - 为什么我的链接样式表中的某些 CSS 格式未应用?
- c# - 有没有办法在 SinglePage 中包含 2 个 Angular 应用程序?
- android - Android:如何断开已连接的蓝牙设备?