在乒乓球比赛中计算胜利

时间:2014-01-31 22:30:33

标签: java methods pong

  

在Scores.java中创建一个带有2个参数的方法:player1Scores和player2Scores。该方法应该打印出player1赢得的游戏数量和玩家2赢得的游戏数量。

这是我们老师给出的作业的最后一部分,我无法弄清楚从哪里开始。我在考虑一个简单的比较每个胜利,但这不是由Player1Score提供的。它只存储正在进行的游戏的当前分数。我需要能够计算所有已发生的游戏。知道如何将其添加到我的代码中吗?这是我目前正在研究的程序的片段。

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Scores {

public static void main (String args[]) 

{


}
static String allScoresFilename = "allscores.txt";

static String lastScoreFilename = "lastscores.txt";

public boolean writeScores(int player1Score, int player2Score) throws IOException {

PrintWriter scores = new PrintWriter (new File ("Score.txt"));

    scores.println("Player 1 \t Player 2 \n " +player1Score+"\t\t\t " +player2Score);

PrintWriter scores1 = new PrintWriter(new BufferedWriter(new FileWriter("Scores Append", true)));

    scores1.println("Player One:"+player1Score+"\tPlayer Two:"+player2Score);

    scores1.close();

    scores.close();

    return true;

}//End writeScores

public boolean appendScores(int player1Score, int player2Score) throws IOException{

    return true;

}//End appendScores

public void readScores() throws FileNotFoundException

{

    File scores = new File ("Scores Append");

    Scanner fileScanner = new Scanner (scores);

    fileScanner.useDelimiter("[\t|,|\n|\r|:]+");

    String players, player1;

    int numbers, i=0;

    int []p1= new int[10];

    int []p2= new int[10];

    while (fileScanner.hasNext())
    {

        players = fileScanner.next();

        p1[i] = fileScanner.nextInt();

        player1 = fileScanner.next();

        p2[i] = fileScanner.nextInt();

        System.out.println ("p1 is currently "+ p1[i]);

        System.out.println ("p2 is currently "+ p2[i]);

        i++;
    }

}//End readScores

public boolean wins(int player1Score, int player2Score) throws IOException

{

    return true;

}//End wins
}

2 个答案:

答案 0 :(得分:0)

您已经拥有readScores功能,可以读取播放器1和播放器2分数的列表。基于此,您可以轻松浏览这些阵列并确定哪个玩家赢得了每场比赛。

答案 1 :(得分:0)

readScores()之外获取这些数组的声明,并将其放在类的顶部,以便您可以使用它们调用wins

int []p1= new int[10];

int []p2= new int[10];

wins的签名更改为:

public void wins(int []player1Scores, int []player2Scores)
{
    int player1Wins = 0;
    int player2Wins = 0;

    for (int i = 0; i < 10; i++)
        (player1Scores > player2Scores) ? player1Wins++ : player2Wins++;

    System.out.printf("Player 1 wins: %-10i Player 2 wins %-10i", player1Wins, player2Wins);
}//End wins

我不会回复任何评论,因为今晚我很忙,但好运查尔斯。