找不到问题,正如您在“readFile”方法中看到的那样,我试图查看问题是否是扫描仪读取了一个字符串,但是当我编译程序时它会说文本文件的名称,scores.txt ,而不是内容。已经为此工作了大约3个小时,dayum。
package pointSystem;
import java.io.*;
import java.lang.*;
import java.util.*;
import main.Gooftw;
public class HighscoreManager {
private int currentHighScores[] = new int[5];
private int newScore = Gooftw.getScore();
private int temp;
//private Formatter formatter;
private Scanner sc;
private String file = "scores.txt";
File outfile;
FileOutputStream fop = null;
public HighscoreManager () {
System.out.println("Hello World!");
try {
//formatter = new Formatter(file);
sc = new Scanner(file);
} catch(Exception e){
System.out.println("you have an error");
}
//Read current highscores to game
}
public void readFile(){
int i = 0;
String values [] = new String [5];
while(sc.hasNext()) {
values[i] = sc.next();
System.out.println(values[i]);
/*System.out.println(sc.nextInt());
currentHighScores[i] = sc.nextInt();
System.out.println(currentHighScores[i]);*/
i++;
}
this.closeFile();
/*for(i=0;i<5;i++) {
System.out.println(currentHighScores[i]);
}
/*while(sc.hasNext()){
String a = sc.next();
String b = sc.next();
String c = sc.next();
System.out.printf(a,b,c);
}*/
}
public void addRecords(){
try{
outfile = new File(file);
fop = new FileOutputStream(outfile);
if(!outfile.exists()){
outfile.createNewFile();
}
for(int i = 0; i<5; i++){
//formatter.format("%s \n", currentHighScores[i] );
fop.write(currentHighScores[i] );
}
//fop.flush();
fop.close();
}catch(IOException e){
e.printStackTrace();
} finally {
try{
if(fop!=null){
fop.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
//formatter.close();
public void compareScores(){
if(newScore > currentHighScores[0]){
temp = currentHighScores[0];
currentHighScores[0] = newScore;
}
for(int y = 1 ; y<5 ; y++){
if(temp < currentHighScores[y]){
temp = temp + currentHighScores[y];
currentHighScores[y] = temp - currentHighScores[y];
temp = temp - currentHighScores[y];
}
}
}
public void closeFile(){
sc.close();
}
}