在乒乓球比赛中数赢 [英] Counting Wins in a Ping Pong game
问题描述
在Scores.java中创建一个带有2个参数的方法:player1Scores和player2Scores.该方法应打印出player1赢得的游戏数和player2赢得的游戏数.
Create a method in Scores.java that takes 2 arguments: player1Scores and player2Scores. The method should print out the number of games player1 won and the number of games player2 won.
这是老师给作业分配的最后一部分,我不知道从哪里开始.我当时在想简单地比较每场胜利,但是那不是Player1Score所能提供的.它仅存储正在进行的游戏的当前分数.我需要能够计算出发生的所有游戏.知道如何将其添加到我的代码中吗?这是到目前为止我正在开发的程序的一个片段.
This is the last part to an assignment given by our teacher and I can't figure out where to start. I was thinking a simple compare each win but thats not provided by the Player1Score. It only stores the current score of the game thats going on. I need to be able to calculate all the games that have happened. Any idea how i could add this to my code? Here is a snippet of the program I'm working on so far.
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
}
推荐答案
将这些数组的声明置于readScores()
之外,并将其放在类的顶部,以便您可以使用它们调用wins
.
Take the declaration for these arrays outside of readScores()
and put it at the top of your class so that you can invoke wins
with them.
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
因为今晚我很忙,所以我不会回复任何评论,但是查尔斯祝你好运.
I won't be replying to any comments on this because I'm super busy tonight, but good luck Charles.
这篇关于在乒乓球比赛中数赢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!