有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多维数组在java中读取文件后将数据值分组

我正在用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) 个答案

  1. # 1 楼答案

    I am at a lost on how to score each line.

    首先处理每一行,即在二维数组中的每一行上创建一个循环

    for (double[] round : darts)
    

    Every two doubles would be passed to the method score()

    因此,在行中的列上循环,每次执行2步

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

    once I got 3 score that would be added for player ones total score and then same for the next 6 doubles

    所以把上半场加到1号球员的总数上,把下半场加到2号球员的总数上

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

    And then repeat until all lines are processed.

    请参见第一个答案,您在其中创建了一个循环