我正在用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对玩家1是3对,第二个6对玩家2是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.
如何处理每条线以得分第一位玩家,然后得分第二位玩家?
编辑
很抱歉没有显示代码,以为问题可以解决。
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个双打中相加。然后重复,直到处理完所有行。 最佳答案
这是我的处理方法:
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
关于java - 从Java中的文件读取后将数据值分成几组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59013461/