比较txt文件中的两个列值 - Java

时间:2016-03-17 15:26:42

标签: java

我的任务是获取一个包含来自" Big Wheel"的结果的.txt文件。来自Price is Right的游戏显示并输出%Player 1,2和3获胜(格式为P1W,P2W和P3W),以及三路(T3)和双向联系(T2)。我的文本文件格式如下:

Plr1 Plr2 Plr3
  55   85   45
  75   75   75
  75   50   75
  75 Over   90
Over   90   95
      ...

我的问题是我不确定如何将column1数据与column2和column3进行比较。这就是我到目前为止所做的:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class BigWheel {

public static void main(String[] args) {

    Scanner fileIn = null;
    PrintWriter fileOut = null;
    String header;
    String aLine;
    String player;
    double total;
    int games;
    int T3;
    int T2;
    int P1W;
    int P2W;
    int P3W;

    /* input file and generate output file */

    try {
        fileIn = new Scanner(new File("BigWheelScores.txt"));

    } catch (FileNotFoundException e) {
        System.out.println("Sorry, I cannot find your input file");
        System.exit(0);

        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fileOut = new PrintWriter("output.txt");
    } catch (FileNotFoundException e) {
        System.out.println("Sorry, I cannot open your output file");
        System.exit(0);

        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    /* data load process */ 

    header = fileIn.nextLine();

    while (fileIn.hasNextLine()) {

        aLine = fileIn.nextLine();

        String[] tokens = aLine.split("\\s+");
        player = tokens[0];


/* calculate wins, ties, and games played */    

    games = 0;
    T3 = 0;
    T2 = 0;
    P1W = 0;
    P2W = 0;
    P3W = 0;


         for (int row = 1; row < tokens.length; row++) {
                for(int col=1; col<4; col++){   
                    if (col[1] == col[2]) && (col[2]) == (col[3])
                            T3++;
                    else 
                        if (((col[1] == col[2]) && (col[1] > col[3])) ||
                                ((col[1] == col[3]) && (col[1] > col[2])) || 
                                ((col[2] == col[3]) && (col[2] > col[1])) )
                                T2++;
                        else
                            if ((col[1] > col[2]) && (col[1] > col[3]))
                            P1W++;
                            else 
                                if ((col[2] > [col[1]) && (col[2] > col[3]))
                                P2W++;
                                else
                                    P3W++;
                }
                games++;
         }


        /* output results */

        System.out.println("Games Played: " + games);
        System.out.println("# of 3 Way Ties: " + T3);
        System.out.println("# of 2 Way Ties: " + T2);
        System.out.println("Player 1 Win %: " + (P1W/games));
        System.out.println("Player 2 Win %: " + (P2W/games));
        System.out.println("Player 3 Win %: " + (P3W/games));

        fileOut.println("Games Played: " + games);
        fileOut.println("# of 3 Way Ties: " + T3);
        fileOut.println("# of 2 Way Ties: " + T2);
        fileOut.println("Player 1 Win %: " + (P1W/games));
        fileOut.println("Player 2 Win %: " + (P2W/games));
        fileOut.println("Player 3 Win %: " + (P3W/games));

    }

    fileIn.close();
    fileOut.close();
}

}

我意识到它可能很简单,但我正在努力想出一种实际实现它的方法。

0 个答案:

没有答案