我想为我的程序做一个小高分。该程序有一个LogFile,我想读取该文件并确定获胜者。因为我的程序有一些问题,你可以一遍又一遍地回答同样的问题,并因此获得学分。使用下面的代码,我试图防止作弊得分。我试着比较第三,第四和第五个字符串,这代表了你必须解决的计算。当下一行具有相同的计算时,我想忽略hilicore的计数有人能帮我一下吗?我只得到一堆错误。
谢谢你的时间和帮助。
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)
如果你继续使用scan。接下来,在条件语句中,扫描器将扫描下一个值。我认为你想要获取下一个值并将其用于比较。
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;
}
我希望这对你有帮助。干杯. . !
在if语句中调用scan.next()是个坏主意。第二次扫描。Next只在第一个等于为真时调用,第二个等于为真时调用,因此很容易混淆要读的列。移动扫描。对于初学者,if语句的下一个。
从错误消息中,您正在调用日期字符串上的Integer.parseInt(i);
。
看起来你正在试图找到文件中最大的数字?你应该做的是存储最高的int值,它在循环外声明。例如
int Highscore = 0;
while (...) {
....
int num = Integer.parseInt(i);
if (num > Highscore) {
Highscore = num;
}
如果你明白我的意思。