1

这是我现在拥有的代码,它完全被屠杀了。我有问题尝试允许用户输入一个字符串和两个双打进入 3 个并行数组,然后保存到 .txt 文件。我不知道出了什么问题,有人可以帮助我吗?

public static void addGames(int i, String[] array1, double[] array2, 
        double[] array3, int arrayLength, Scanner keyboard) throws IOException
{
    String newName;
    double newPrice;
    double newRating;

    if(i < arrayLength)
    {
        System.out.println("Please enter another game name: ");
        newName = keyboard.next();
        array1[i] = newName;
        System.out.println("Please enter another game price: ");
        newPrice = keyboard.nextDouble();
        array2[i] = newPrice;
        System.out.println("Please enter another game rating: ");
        newRating = keyboard.nextDouble();
        array3[i] = newRating;
        i++;
    }
    else
    {
        System.out.println("There is no more room to store games: ");
    }

    PrintWriter gamerOut = new PrintWriter("Project1_VideoGames.txt");

    while(i < array1.length)
    {
        gamerOut.write(array1[i]);
        gamerOut.add(array2[i]);
        gamerOut.add(array3[i]);
        i++;
    }
    gamerOut.close();

}
4

2 回答 2

0
for (int j = 0; j < i; ++j) {
    gamerOut.println(array1[j] + "\t" + array2[j] + "\t" + array3[j]);

不用说这些名字对我来说太有想象力了。做一个

public class Game {
    String name;
    double price;
    double rating;
}
于 2014-02-07T15:36:15.690 回答
0

检查这是否是您想要的。

我没有使用 3 个数组,而是将所有数组封装在一个Game类中。

public class Game {
    private String name;
    private double price;
    private double rating;

    public Game(String name, double price, double rating){
        this.name = name;
        this.price = price;
        this.rating = rating;
    }

    @Override
    public String toString(){
        String ret = "";
        ret = ret + name + " / " + price + " / " + rating;

        return ret;
    }   
}

这就是我为你的addGames功能带来的。它现在只需要 1 个参数:您要在文件中写入的游戏数量。

public static void addGames(int gamesNumber) throws IOException
    {
        int i = 0;
        String newName;
        double newPrice, newRating;
        Scanner keyboard = new Scanner(System.in);
        ArrayList<Game> array = new ArrayList<Game>();

        while(i < gamesNumber)
        {

            System.out.println("Please enter another game name: ");
            newName = keyboard.next();
            System.out.println("Please enter another game price: ");
            newPrice = keyboard.nextDouble();
            System.out.println("Please enter another game rating: ");
            newRating = keyboard.nextDouble();
            System.out.println();

            Game game = new Game(newName, newPrice, newRating);
            array.add(game);

            i++;
        }

        System.out.println("There is no more room to store games. ");

        PrintWriter gamerOut = new PrintWriter("Project1_VideoGames.txt");
        i = 0;

        while(i < array.size())
        {
            gamerOut.println(array.get(i));
            i++;
        }

        gamerOut.close();

        System.out.println("The games have been written in the file");
    }

您可能希望在读取用户输入或处理异常时处理一些错误,FileWriter但我将把它留给您。

此外,我已更改为PrintWriter#println方法,而不是PrintWriter#write覆盖类toString中的方法Game。您可能也想更改它的实现。

希望这有帮助。

于 2014-02-07T16:11:10.677 回答