【问题标题】:Separate data value into groups after reading from file in java从java中读取文件后将数据值分组
【发布时间】:2026-01-07 01:50:02
【问题描述】:

我正在读取 java 中的文件并将其添加到多维数组中。

文件如下所示:

14.9 18.9 -18.6 -7.7 11.5 0.6 -5.3 -15.5 -1.5 -12.5 19.2 -8.8
2.9 -2.9 13.4 -12.8 -9.8 6.2 11.0 0.9 3.7 17.3 -0.2 -18.7
18.5 -2.8 6.7 16.5 7.4 -17.5 17.8 -14.3 -11.2 14.6 7.9 -9.7
7.0 16.8 12.9 -0.2 -7.4 0.7 8.6 11.8 3.4 5.7 3.1 -15.6
-12.2 -5.4 -4.1 -15.6 -14.8 -5.1 -0.6 4.2 14.1 13.5 13.2 -9.9
1.6 2.1 11.5 9.1 -13.8 -3.9 15.8 15.4 8.9 16.8 6.8 20.0
13.7 -12.6 12.2 1.1 18.7 -19.0 -11.4 -10.7 -2.9 -0.6 -9.5 0.4
14.2 -3.3 6.8 -5.9 -4.5 12.2 19.3 -13.0 -3.7 14.5 6.9 14.6
1.9 -10.7 19.2 -3.7 -10.7 -18.5 3.1 -16.5 10.6 -1.9 -17.7 -15.3
-17.4 0.2 4.1 1.1 -12.1 -20.0 -10.4 -9.3 -19.0 15.1 -3.7 -16.7
-14.9 -6.9 -5.9 -11.9 -15.7 -19.7 -8.1 -3.8 8.5 8.0 17.3 9.7
-4.0 -5.4 16.0 7.8 19.6 -7.0 0.2 9.3 -9.4 13.1 -5.3 -7.0
-14.1 -11.8 -6.6 8.5 16.2 -13.6 6.4 2.0 17.0 -0.5 -10.6 18.2
-1.0 18.5 9.1 13.4 6.6 -3.7 -15.1 11.8 10.7 -8.1 14.1 12.8
-2.7 1.2 -3.1 17.0 6.2 14.5 5.3 -12.9 -5.6 12.2 10.5 -17.1
-19.6 11.0 6.1 15.2 5.1 5.7 6.5 -19.6 -18.7 11.2 -1.2 11.0
-5.9 17.9 -15.4 -2.5 8.4 16.8 -2.3 -13.6 -12.5 16.1 -19.6 -16.5
-4.4 2.4 -6.6 3.1 6.3 16.6 -1.5 11.6 14.3 -15.5 -12.7 -11.7
5.2 4.1 -2.1 -18.9 -16.0 1.9 17.3 12.7 -2.8 -3.4 -3.3 12.0
-12.1 5.2 -12.8 -19.9 -11.8 -17.1 -14.6 3.4 -7.1 12.7 8.3 0.5
3.5 2.7 -3.1 -9.9 17.1 2.3 18.1 -8.8 -2.8 -14.2 -4.6 17.3
0.2 18.7 -16.8 18.5 4.5 -7.8 -1.3 -10.0 5.8 -11.4 -11.5 12.4
19.3 13.2 -6.7 3.3 -17.9 -2.4 -6.8 -11.8 9.5 -13.1 -0.7 -3.1
-3.3 6.2 1.3 -11.3 6.2 -5.9 -11.7 14.4 -11.4 0.8 0.1 2.0
-2.0 -8.1 -13.4 13.9 -6.3 -13.3 -14.0 17.9 10.1 19.9 -5.3 -13.3
1.2 -3.5 -2.5 5.5 -12.8 -16.7 -9.7 -18.7 5.5 5.4 -3.8 13.3
-15.5 5.3 -9.4 -19.7 -4.7 -0.8 17.8 3.6 -12.5 -1.7 13.8 -8.2
3.0 12.4 14.8 -19.4 16.2 -3.8 7.4 -6.4 -8.2 5.2 -1.5 18.9
-13.0 12.6 -0.8 -1.3 11.2 -0.4 10.3 -11.8 -4.4 3.2 9.0 -0.1
7.2 -17.0 11.3 14.9 -14.7 18.7 19.6 13.7 13.4 -16.3 7.5 -1.2

每条线代表两个玩家的飞镖游戏。前 6 个是玩家一的 3 对,后 6 个是玩家二的 3 对。

程序读取文件,然后根据每一行吐出分数。

例子:

input: (14.9, 18.9) (-18.6, -7.7) (11.5, 0.6) (-5.3, -15.5) (-1.5, -12.5) (19.2, -8.8)
output: Player two wins: Player one score 280, Player two score 290.

我如何处理每条线来得分第一个球员,然后得分第二个球员投掷?

编辑

抱歉没有显示代码,以为问题可以不用out来回答。

public class DartBoard {
    private double[][] darts;

    public DartBoard(String file) throws FileNotFoundException {
        Scanner scan = new Scanner(new File(file));
        int numberRows = 12;
        int numberCols = 30;

        darts = new double[numberRows][numberCols];
        for(int i = 0; i < numberRows; i++){
            for(int j = 0; j < numberCols; j++){
                if(scan.hasNextDouble()){
                    darts[i][j] = scan.nextDouble();
                }
            }
        }
    }

    public double distance(double x, double y){
        return Math.sqrt(x*x + y*y);
    }

    public int score(double x, double y){
        double dist = distance(x,y);
        int s = 0;
        if(dist < 4){
            s = 100;
        } else if(dist < 8){
            s = 80;
        } else if(dist < 12){
            s = 60;
        } else if(dist < 16){
            s = 40;
        } else if(dist < 20){
            s = 20;
        }
        return s;
    }
}

我不知道如何为每条线打分。每两个双打将传递给方法score(),一旦我得到3分,将添加到玩家的总分中,然后在接下来的6个双打中相同。然后重复直到处理完所有行。

【问题讨论】:

  • 你试过什么?告诉我们你有什么。你被困在哪里了?你能:1)阅读文件吗? 2)从文件中读取行? 3) 将一行拆分为 12 个字符串? 4)将它们解析为double? 5) 配对? 6) 将玩家 1 的对子分成 3 对玩家 2 和玩家 2 的对子? 7)如果你能做到所有这些,那么问题是什么? --- 投票结束时“太宽泛”,也就是“需要更多关注”,因为这个问题现在太开放了,这意味着看起来您希望我们为您编写代码,而这不是 *是关于。
  • 给我们看一些代码!...
  • @Andreas 我添加了我已经拥有的代码。我对如何将价值分配给每个玩家感到非常困惑。 txt 文件有一行 12 个双精度数,这就是我将它添加到多维数组的方式。
  • @Idle_Mind 添加我现在拥有的工作代码。只是不知道下一步将如何读取要评分的每一行以及如何处理!
  • 对我来说,您的文件看起来有 30 行 12 列,那为什么代码有 int numberRows = 12; int numberCols = 30;

标签: java multidimensional-array java.util.scanner


【解决方案1】:

以下是我的处理方式:

import java.io.File;
import java.util.Scanner;
import java.text.DecimalFormat;
public class DartBoard
{

    public static void main(String[] args)
    {
        String fileName = "C:\\Users\\mikes\\Documents\\darts.txt";
        try {
            DartBoard db = new DartBoard(fileName);
            db.DisplayScores();
        } catch (Exception ex) {
            ex.printStackTrace();
        }                             
    }

    private double[][] darts;

    public DartBoard(String file) throws Exception {
        // we are going to read the file TWICE

        // figure out how many rows/cols we have:
        Scanner scan = new Scanner(new File(file));             
        int numberRows = 0;
        int numberCols = -1;
        while(scan.hasNextLine())
        {
            String row = scan.nextLine();
            numberRows++;
            String[] columns = row.split(" ");
            if (numberCols == -1)
            {
                numberCols = columns.length;
            }
            else if (columns.length != numberCols)
            {
                throw new IllegalArgumentException("Number of columns in each row does not match!");
            }
        }

        // instantiate the array and populate it:
        darts = new double[numberRows][numberCols];
        scan = new Scanner(new File(file));
        for(int i = 0; i < numberRows; i++){
            for(int j = 0; j < numberCols; j++){
                if(scan.hasNextDouble()){
                    darts[i][j] = scan.nextDouble();
                }
            }
        }
    }

    public void DisplayScores() throws Exception
    {
        if (darts != null)
        {
            int rows = darts.length;
            int cols = darts[0].length;
            if (cols % 2 == 0)
            {
                System.out.println("Scoring Results:");
                DecimalFormat f = new DecimalFormat("000");
                for(int r = 0; r < rows; r++)
                {
                    int player1Score = 0;
                    int player2Score = 0;
                    for(int c = 0; c < cols; c += 2)
                    {
                        if (c < cols / 2)
                        {
                            player1Score += score(darts[r][c], darts[r][c+1]);
                        }
                        else
                        {
                            player2Score += score(darts[r][c], darts[r][c+1]); 
                        }
                    }
                    String winner = (player1Score == player2Score ? "Tie" : player1Score > player2Score ? "One" : "Two");
                    System.out.println("Winner: Player " + winner + "  |  Player One Score: " + f.format(player1Score) + "  |  Player Two Score: " + f.format(player2Score));
                }
            }
            else
            {
                throw new IllegalArgumentException("Number of columns cannot be odd!");
            }
        }
        else
        {
            throw new IllegalArgumentException("No data to work with!");
        }
    }

    private double distance(double x, double y){
        return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    }

    private int score(double x, double y){
        double dist = distance(x,y);
        int s = 0;
        if(dist < 4){
            s = 100;
        } else if(dist < 8){
            s = 80;
        } else if(dist < 12){
            s = 60;
        } else if(dist < 16){
            s = 40;
        } else if(dist < 20){
            s = 20;
        }
        return s;
    }

}

输出:

Scoring Results:
Winner: Player Tie  |  Player One Score: 060  |  Player Two Score: 060
Winner: Player One  |  Player One Score: 160  |  Player Two Score: 100
Winner: Player Tie  |  Player One Score: 060  |  Player Two Score: 060
Winner: Player Two  |  Player One Score: 140  |  Player Two Score: 160
Winner: Player Two  |  Player One Score: 100  |  Player Two Score: 120
Winner: Player One  |  Player One Score: 180  |  Player Two Score: 020
Winner: Player Two  |  Player One Score: 060  |  Player Two Score: 200
Winner: Player One  |  Player One Score: 140  |  Player Two Score: 060
Winner: Player Tie  |  Player One Score: 080  |  Player Two Score: 080
Winner: Player One  |  Player One Score: 100  |  Player Two Score: 060
Winner: Player Two  |  Player One Score: 060  |  Player Two Score: 140
Winner: Player Two  |  Player One Score: 100  |  Player Two Score: 140
Winner: Player Two  |  Player One Score: 080  |  Player Two Score: 100
Winner: Player One  |  Player One Score: 120  |  Player Two Score: 080
Winner: Player One  |  Player One Score: 160  |  Player Two Score: 080
Winner: Player One  |  Player One Score: 100  |  Player Two Score: 060
Winner: Player One  |  Player One Score: 080  |  Player Two Score: 040
Winner: Player One  |  Player One Score: 180  |  Player Two Score: 080
Winner: Player Tie  |  Player One Score: 120  |  Player Two Score: 120
Winner: Player Two  |  Player One Score: 040  |  Player Two Score: 140
Winner: Player One  |  Player One Score: 160  |  Player Two Score: 060
Winner: Player Two  |  Player One Score: 080  |  Player Two Score: 120
Winner: Player Two  |  Player One Score: 100  |  Player Two Score: 160
Winner: Player One  |  Player One Score: 200  |  Player Two Score: 180
Winner: Player One  |  Player One Score: 120  |  Player Two Score: 040
Winner: Player One  |  Player One Score: 180  |  Player Two Score: 120
Winner: Player One  |  Player One Score: 100  |  Player Two Score: 080
Winner: Player Two  |  Player One Score: 060  |  Player Two Score: 140
Winner: Player Tie  |  Player One Score: 180  |  Player Two Score: 180
Winner: Player Two  |  Player One Score: 040  |  Player Two Score: 080

【讨论】:

  • 哇!这是一项了不起的工作!正是我想要达到的目标。好奇为什么在这里使用 DecimalFormat?
  • DecimalFormat 用于用前导零填充分数,80 --> 080,以便所有内容始终排列在漂亮的列中。
  • 啊,是的,我现在看到了!好奇你计算每一行背后的思维过程。这是你以前遇到过并且已经知道应该如何处理的事情吗?
  • 确实如此。由于我们正在处理成对的值,因此我们必须确保我们有偶数列。然后我们必须通过将计数器变量增加 2 而不是 1 来跳过已经读取的第二个值。最后,前半部分的分数加到玩家一,而后半部分的分数加到玩家二。这些都是 Andreas 在他的帖子中提到的。
【解决方案2】:

我不知道如何为每条线打分。

从处理每一行开始,即在二维数组的每一行上创建一个循环。

for (double[] round : darts)

每两个双精度数将传递给方法score()

因此循环遍历行中的列,一次执行 2 步。

for (int i = 0; i < round.length; i += 2)
    score = score(round[i], round[i + 1])

一旦我得到 3 分,将添加到球员的总分中,然后在接下来的 6 次双打中相同

所以将前半部分添加到玩家 1 的总数中,并将后半部分添加到玩家 2 的总数中

if (i < row.length / 2)
    totalPlayer1 += score
else
    totalPlayer2 += score

然后重复直到处理完所有行。

查看第一个答案,您在其中创建了一个循环。

【讨论】:

  • 好吧,这确实很好,但它会将所有回合作为累积总数添加到玩家类中。我需要一种方法来区分回合得分,以便查看谁赢得了该回合。
  • @AlexanderMcComb 为什么你认为totalPlayer1totalPlayer2 应该在第一个for 循环外部 声明和初始化?考虑一下如果在循环内部声明和初始化它们会发生什么。
  • for(double[] round : darts){ for(int i = 0; i
  • @AlexanderMcComb 不知道那应该是什么,我似乎在问题的代码中找不到p1p2,所以我不知道发生了什么继续那个p1.setScore(s) 电话。它确实看起来不像我展示的totalPlayer1 += score 逻辑的良好替代品,因为setXxx 方法替换当前值,而+= 操作增加 当前值。 --- 声明和初始化totalPlayer1inside第一个(即外部)循环的哪一部分你不明白?