【发布时间】:2015-01-12 20:55:06
【问题描述】:
我正在编写一个程序,它读取包含匹配结果的文本文件,然后将它们输出到表格中。我在 While 循环中有一个 While 循环:
Scanner fileread1 = new Scanner(new File("demo.txt"));
int x = 0;
int y = 22;
int i = 0;
while (x <= y) {
while (fileread1.hasNext()) {
fileinput = fileread1.nextLine(); // this reads the next line of
// from the file
String line = fileinput;
String[] split = line.split(":");
boolean result = false;
int homescore1 = 0;
int awayscore1 = 0;
int goalsscored = 0;
boolean att = false;
boolean htt = false;
int atscore = 0;
int htscore = 0;
// When the text line is read, it is then split into four sections.
if (split.length == 4) {
// String text = line.trim();
String userteam = userteaminput;
String hometeam = split[0].trim();
String awayteam = split[1].trim();
String home_score = split[2].trim();
String away_score = split[3].trim();
// this is a test to try convert the goals string into a
// integer. If this fails, the result is not
// not valid and does not get outputted to the console.
try {
homescore1 = Integer.parseInt(home_score);
awayscore1 = Integer.parseInt(away_score);
result = true;
}
catch (NumberFormatException exception) {
// if the try is not able to convert, this will run
errors++;
}
if (userteam.equals(Teams.get(i))) {
if (awayteam.equalsIgnoreCase(userteam)) {
att = true;
games++;
goalsfor = goalsfor + awayscore1;
goalsagainst = goalsagainst + homescore1;
}
if (att == true && awayscore1 > homescore1) {
atwc++;
gameswon++;
}
else if (att == true && awayscore1 < homescore1) {
htwc++;
gameslost++;
}
else if (att == true && awayscore1 == homescore1) {
gamesdrawn++;
}
if (hometeam.equalsIgnoreCase(userteam)) {
htt = true;
totaluser++;
games++;
goalsfor = goalsfor + homescore1;
goalsagainst = goalsagainst + awayscore1;
}
if (htt == true && homescore1 > awayscore1) {
atwc++;
gameswon++;
}
else if (htt == true && homescore1 < awayscore1) {
htwc++;
gameslost++;
}
else if (htt == true && awayscore1 == homescore1) {
gamesdrawn++;
}
}
}
else {
errors++;
}
}
// ********************************************************************
// Leeds IF Statement
// ********************************************************************
if (Rhinos.equals(Teams.get(i)) {
Rhinos.goalsfor = Rhinos.goalsfor + goalsfor;
Rhinos.gameswon = Rhinos.gameswon + gameswon;
Rhinos.gameslost = Rhinos.gameslost + gameslost;
Rhinos.goalsagainst = Rhinos.goalsagainst;
Rhinos.gamesplayed = Rhinos.gamesplayed + games;
}
else if (Bulls.equals(Teams.get(i)) {
Bulls.goalsfor = Bulls.goalsfor + goalsfor;
Bulls.gameswon = Bulls.gameswon + gameswon;
Bulls.gameslost = Bulls.gameslost + gameslost;
Bulls.goalsagainst = Bulls.goalsagainst;
Bulls.gamesplayed = Bulls.gamesplayed + games;
}
x++;
i++;
goalsfor = 0;
gameswon = 0;
gameslost = 0;
gamesagainst = 0;
}
我知道在提供的文本文件中只有 22 个团队有结果,所以第一个循环应该运行 22 次。
当提供的文件有下一行时,内部循环将继续。文本文件有时可能比其他文件有更多的结果行。在这个循环中,我引用了一个数组项:
if (userteam.equals(Teams.get(i)))
在第一次运行中,这将引用我的 Array 中的 0,作为记录,它是 Leeds Rhinos。一旦内循环完成,它就会移动到外循环 - 这处理刚刚记录的结果。如果当前团队是 Leeds Rhinos,则应添加值。然后 i 应该添加 1,因此对于下一个循环,它指的是数组 1 的索引,而不是 0。(我这里有更多 IF 语句,所有相同但参考其他团队)变量设置回 0 ,为下一次运行做好准备。
我遇到的问题是,i 似乎每次运行时都没有添加 1,所以我只得到一个团队的结果。如果我手动指定要查看的数组索引(比如 3),它将运行,团队将成功记录他们的结果。
有没有一种方法可以在每次循环时将 1 添加到 i 中?我不确定这是否是要使用的正确 java 循环,但对我来说,这似乎是最合乎逻辑的。这里没有声明一些对象 - 这只是代码的 sn-p,省略了声明,因为我知道它们可以工作,并且声明了很多。
【问题讨论】:
-
第一个循环中 X 的增量在哪里?!
-
已添加 - 在整理代码以在此处发布时必须删除它!谢谢!
-
请确保您的花括号是正确的,并且您问题中的花括号与您的代码中的花括号相匹配。看起来它们可能是错误的,但如果您的实际代码中的大括号与我们看到的不同,就很难判断发生了什么。
-
我不在乎别人怎么说:埃及花括号很难阅读。这不是带有 100-500mb 硬盘的 80 年代/90 年代。硬盘现在很大。无需通过在 if 语句和循环的开头保留
{之前的换行符来节省空间。 -
@developerwjk 硬盘大小不是最大的因素;最大的限制是屏幕上可以容纳多少行。由于行数有限,我的偏好是不要用太多的行来保存
{;我宁愿看到更多的逻辑而不必滚动。再说一次,当它们有帮助时,我仍然在我的代码中添加空行。这是一种权衡,在哪里取得平衡取决于个人喜好。
标签: java loops while-loop nested-loops