gpt4 book ai didi

java - Java 文件未找到异常

转载 作者:行者123 更新时间:2023-12-01 15:57:56 25 4
gpt4 key购买 nike

我正在尝试为扫雷游戏制作一个简单的高分系统。但是,我不断收到文件未找到的异常,并且我也尝试使用该文件的完整路径。

package minesweeper;

import java.io.*;
import java.util.*;

public class Highscore{

public static void submitHighscore(String difficulty) throws IOException{
int easy = 99999;
int normal = 99999;
int hard = 99999;
//int newScore = (int) MinesweeperView.getTime();
int newScore = 10;
File f = new File("Highscores.dat");

if (!f.exists()){
f.createNewFile();

}
Scanner input = new Scanner(f);
PrintStream output = new PrintStream(f);

if (input.hasNextInt()){
easy = input.nextInt();
normal = input.nextInt();
hard = input.nextInt();
}

output.flush();



if(difficulty.equals("easy")){
if (easy > newScore){
easy = newScore;
}
}else if (difficulty.equals("normal")){
if (normal > newScore){
normal = newScore;
}
}else if (difficulty.equals("hard")){
if (hard > newScore){
hard = newScore;
}
}
output.println(easy);
output.println(normal);
output.println(hard);

}

//temporary main method used for debugging

public static void main(String[] args) throws IOException {
submitHighscore("easy");
}

}

最佳答案

您没有透露异常是在哪一行代码上发出的​​。 (注意:不发布您所掌握的有关该问题的所有信息会降低您获得有用答案的机会。)

但是,我的预感是它来自如下所示的第二次调用,在这种情况下,问题在于尝试打开文件两次:

Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);

关于java - Java 文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4689681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com