java - 我不认为我的数组正在存储和数据
问题描述
我是 Java 新手,现在正在上大学课程。我只是好奇我缺少什么来给自己一个输出,因为现在控制台是空白的。
public static void main(String[] args) throws IOException {
double avgScore;
int highestScore, count = 0;
String highScoringStudent;
String names[] = null;
int scores[] = null;
File myFile = new File("Student_Data.txt");
Scanner inFile = new Scanner(myFile);
inFile.nextLine();
while(inFile.hasNext()) {
for(int i = 0; i > 0; i++) {
names[i] = inFile.next();
scores[i] = inFile.nextInt();
count++;
}
}
System.out.print("Name Score");
System.out.print(names[count] + " " + scores[count]);
}
解决方案
首先,我真的不建议将 afor
放在while
循环内,尤其是在这种情况下(因为它不起作用)。以下是问题:
1)您的for
循环从i = 0开始,并立即结束,因为!(i > 0) (i is not > 0) - 所以你是对的,没有数据将存储在数组中!
2)在你的for
循环中,你正在读取一个字符串,然后一个一个地读取一个 int。阅读完它们后,您应该移动到下一个字符串和整数以存储在names
and的下一个位置score
。
3)您没有增加 i: 所以(如果 ifor
正在工作),您只需在同一位置继续为数组分配不同的值(基本上每次都重置变量的值)。
这将是最适合您的情况的代码:
int i = 0;
while(inFile.hasNext()) {
names[i] = inFile.next();
scores[i] = inFile.nextInt();
count++;
i++;
}
希望这有帮助!:)
推荐阅读
- ms-access - Microsoft Access 程序中的这行 Visual Basic 代码是什么意思?
- charts - 在 ng2-google-charts 中调整图表大小
- android - 如何使用 react-native 通过 WebView 下载?
- jenkins - 是否可以使用 PlasticSCM Jenkins 插件制作部分工作区?
- node.js - 将环境变量从 grunt 传递到文件
- php - 无法在 csv 文件的不同列中写入不同的项目
- c# - 如何在 IdentityServer 3 的 AllowedCustomGrantTypes 中指定用户定义的名称
- java - 如何使用kafka流处理块/批处理数据?
- mysql - 为什么从 MySQL 5.5 迁移到 5.7 拒绝连接?
- entity-framework - 将 EntityFramework 设置到另一个配置文件