gpt4 book ai didi

java - 如何用我的主类修复我的 loadData 方法

转载 作者:行者123 更新时间:2023-11-30 05:41:45 26 4
gpt4 key购买 nike

我正在尝试从 txt 文件加载数据,但它只会读取 txt 文件的一行。当我在 loadData 方法的 for 循环中指定 int I 变量的内容时,它将打印该特定行。我不知道为什么它不只是添加和打印我的所有数据。

我尝试使用外部 for 循环来查看是否可以以这种方式打印和添加数据,但没有运气

import java.io.*;
import java.util.*;
public class BingoSortTest
{
static BingoPlayer [] test;
public static void main (String [] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
test = new BingoPlayer [10];
loadData();

System.out.print(Arrays.toString(test));


}
public static void loadData() throws IOException
{
Scanner S = new Scanner(new FileInputStream("players.txt"));
double houseMoney = S.nextDouble();
S.nextLine();
int player = S.nextInt();
S.nextLine();

for(int i = 0; i < test.length; i++)
{
String line = S.nextLine();
String [] combo = line.split(",");
String first = combo [0];
String last = combo [1];
double playerMoney = Double.parseDouble(combo[2]);
BingoPlayer plays = new BingoPlayer(first, last, playerMoney);
add(plays);
}

}
public static void add(BingoPlayer d)
{
int count = 0;
if (count< test.length)
{
test[count] = d;
count++;
}
else
System.out.println("No room");
}
}

这是我正在使用的txt文件的内容:

  • 50.00
  • 10
  • 詹姆斯·史密斯,50.0
  • 迈克尔·史密斯,50.0
  • 罗伯特·史密斯,50.0
  • 玛丽亚·加西亚,50.0
  • 大卫·史密斯,50.0
  • 玛丽亚·罗德里格斯,50.0
  • 玛丽·史密斯,50.0
  • 玛丽亚·埃尔南德斯,50.0
  • 玛丽亚·马丁内斯,50.0
  • 詹姆斯·克拉珀,50.0

最佳答案

每次将 BingoPlayer 放置在索引 0 时。

public static void add(BingoPlayer d)
{
int count = 0; // <-------------------- Here
if (count< test.length)
{
test[count] = d;
count++;
}
else
System.out.println("No room");
}

您必须在定义 BingoPlayer 数组的位置定义静态计数器变量。

定义计数变量静态

static BingoPlayer [] test;
static int count = 0;

并像这样修改添加函数定义。

public static void add(BingoPlayer d)
{
if (count< test.length) {
test[count] = d;
count++;
}
else
System.out.println("No room");
}

关于java - 如何用我的主类修复我的 loadData 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55508237/

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