我正在努力为我的程序制作一个Highscore。该程序有一个LogFile,我想读取该文件并确定获胜者。因为我的程序有一些问题,你可以一遍又一遍地回答同一个问题,并获得它的学分。 随着代码吼叫我试图防止作弊得分。我试图比较第三,第四和第五个字符串,它代表你必须解决的计算。当下一行具有相同的计算时,我想忽略高分的计数 有人可以帮我弄这个吗 ?我刚收到一堆错误。
感谢您的时间和帮助。
public int readFile() {
String score = null;
while (scan.hasNext()) {
String a = scan.next();
String b = scan.next();
String c = scan.next();
String d = scan.next();
String e = scan.next();
String f = scan.next();
String g = scan.next();
String h = scan.next();
String i = scan.next();
if (c.equals(scan.next()) && d.equals(scan.next()) && e.equals(scan.next())) {
} else {
temp = Integer.parseInt(i);
if (temp > highscore) {
highscore = temp;
}
}
}
return highscore;
}
文本文件:
014/07/29 08:25:15 95 + 80 4 false -171 0
2014/07/29 08:25:15 95 + 80 4 false -171 0
2014/07/29 08:25:49 8 * 3 24 true 0 1
2014/07/29 08:25:49 8 * 3 24 true 0 2
2014/07/29 08:25:49 8 * 3 24 true 0 3
2014/07/29 08:25:50 8 * 3 24 true 0 4
2014/07/29 08:25:50 8 * 3 24 true 0 5
错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2014/07/28"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at mathe.Highscore.readFile(Highscore.java:42)
at mathe.Gui$TestActionListener.actionPerformed(Gui.java:231)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:0)
在if语句中调用scan.next()是个坏主意。第二个scan.next只有在第一个等于为真时才会被调用,而对于第二个等等则是如此,因此很容易混淆您尝试阅读的列。将那些scan.next移出启动器的if语句。
从错误消息中,您在日期字符串上调用Integer.parseInt(i);
。
看起来你正试图找到文件中最大的数字?你应该做的是存储最高的int,它在循环之外声明。 e.g。
int Highscore = 0;
while (...) {
....
int num = Integer.parseInt(i);
if (num > Highscore) {
Highscore = num;
}
如果你得到我的漂移。
答案 1 :(得分:0)
如果您在条件语句中继续使用scan.next,扫描仪将扫描下一个值。我想你想获得下一个价值并用于比较。
public int readFile() {
String score = null;
while (scan.hasNext()) {
String a = scan.next();
String b = scan.next();
String c = scan.next();
String d = scan.next();
String e = scan.next();
String f = scan.next();
String g = scan.next();
String h = scan.next();
String i = scan.next();
String nextVal = scan.next();
if (c.equals(nextVal) && d.equals(nextVal) && e.equals(nextVal)) {
} else {
temp = Integer.parseInt(i);
if (temp > highscore) {
highscore = temp;
}
}
}
return highscore;
}
我希望这会有所帮助。 干杯..!