我在total played
创建一个程序,该程序读取文本文件并显示各种输出,例如total won
和total played
。
三个输出中的两个我必须完美地显示total won
和return players
,但我无法使最后一个工作,total played
需要total won
和List<Double> RP = data.RegexCheck(fileData.toString(),
"(Return Players: £) (totalPlayed/totalWon)*100",2);
//ReturnPlayers
double ReturnPlayers = 0;
for(double d : RP){
ReturnPlayers+= d;
}
来生成输出。
我拥有的是:
return players
我得到(totalPlayed/totalWon)*100
为try(BufferedReader br = new BufferedReader(new FileReader("myFile.txt"))){}
的事实。我该怎么做呢?
修改
我读取文本文件的方式是
System.out.println("The total times played is: " + totalPlayed);
System.out.println("The total times won is: " + totalWon);
System.out.println("Return Players value is: " + ReturnPlayers);
输出的编写方式是
RP
编辑#2
它正常工作,但最终输出0.0
不显示任何内容,只显示1240
,我觉得它应该显示一个数字,因为它在两个输出中显示分别显示数字320
和387.5
,然后再乘以100,这样可以显示myFile.txt
编辑#3
Start Game
Game number: 1
Player: lose
End Game
Start Game
Game number: 2
Player: lose
End Game
Start Game
Game number: 3
Player: lose
End Game
Start Game
Game number: 4
Player: won
End Game
Start Game
Game number: 5
Player: won
End Game
Start Game
Game number: 6
Player: lose
End Game
Start Game
Game number: 7
Player: won
End Game
Start Game
Game number: 8
Player: won
End Game
的一个例子是
var query = from r in Ctx.Reg
select new
{
IdReg = r.Id,
...
Status1 = r.Hist.OrderByDescending(o => o.Id).Take(1).Any(x=> x.Status == 2),
Status2 = r.Hist.OrderByDescending(o => o.Id).Skip(1).Take(1).Any(x=> x.Status == 5)
};
答案 0 :(得分:1)
Java代码
try(BufferedReader br = new BufferedReader(new FileReader("myFile.txt"))){
String line = null;
int wonCounter = 0;
int lostCounter = 0;
while((line=br.readLine()) != null){
if(line.contains("Player")){
if(line.contains("won")){
wonCounter++;
}else{
lostCounter++;
}
}
}
System.out.println("The total times played is: " + (wonCounter+lostCounter));
System.out.println("The total times won is: " + wonCounter);
System.out.println("Return Players value is: " + (wonCounter+lostCounter)*100.0/wonCounter);
} catch (IOException e) {
e.printStackTrace();
}
输出:
The total times played is: 8
The total times won is: 4
Return Players value is: 200.0