我正在打印到文件,但输出看起来不像所需的输出。它只显示输出中的一个击球手,而不是全部3.我尝试将打印机向上移动,但这也没有产生正确的输出。还有什么我可以尝试或做的,以便从stats.txt文件中打印出其他两个击球手吗?
我的代码
import java.io.*;
import java.util.*;
public class marioSlugger {
public static int getStat(String letter, String stats) {
int count = stats.length() - stats.replace(letter, "").length();
return count;
}
public static void main(String[] args) throws FileNotFoundException {
String hitsLeader = null;
String walksLeader = null;
String hitsByPitchLeader = null;
String strikeoutsLeader = null;
String battingAverageLeader = null;
String obpLeader = null;
int maxHits = 0;
int maxWalks = 0;
int maxHitsByPitch = 0;
int maxStrikeouts = 0;
int maxBa = 0;
int maxobp = 0;
java.io.File file = new java.io.File("stats.txt");
Scanner input = new Scanner(file);
//Find out number for each category
while (input.hasNextLine()) {
String line = input.nextLine();
String[] parts = line.split(" ");
int hits = getStat("H", parts[1]);
int outs = getStat("O", parts[1]);
int walks = getStat("W", parts[1]);
int hitsByPitch = getStat("P", parts[1]);
int strikeouts = getStat("K", parts[1]);
int sacrifices = getStat("S", parts[1]);
int totalAtBats = hits + outs + strikeouts;
int obptotal = hits + outs + walks + hitsByPitch + strikeouts +sacrifices;
int accuracy = 1000;
double ba = ((double)hits/(double)totalAtBats );
double obp = (((double)hits + (double)walks + (double)hitsByPitch) / (double)obptotal);
//System.out.println(parts[0]);
//System.out.printf("BA: %.3f\n", ba);
//System.out.printf("OB: .%d\r\n", (int)(obp * accuracy) );
//System.out.println("H: " + hits);
//System.out.println("BB: " + walks);
//System.out.println("K: " + strikeouts);
//System.out.println("HBP: " + hitsByPitch);
//System.out.println("");
if (hits > maxHits) {
maxHits = hits;
hitsLeader = parts[0];
}
if (walks > maxWalks) {
maxWalks = walks;
walksLeader = parts[0];
}
if (hitsByPitch > maxHitsByPitch) {
maxHitsByPitch = hitsByPitch;
hitsByPitchLeader = parts[0];
}
if (strikeouts > maxStrikeouts) {
maxHits = hits;
strikeoutsLeader = parts[0];
}
if (ba > maxBa) {
maxHits = hits;
battingAverageLeader = parts[0];
}
if (obp > maxobp) {
maxobp = (int) obp;
obpLeader = parts[0];
}
//Create PrintWriter
java.io.PrintWriter output = new java.io.PrintWriter("leaders.txt");
//Print LEAGUE LEADERS to leaders.txt
output.println(parts[0]);
output.printf("BA: %.3f\r\n", ba);
output.printf("OB: .%d\r\n", (int)(obp * accuracy) );
output.println("H: " + hits);
output.println("BB: " + walks);
output.println("K: " + strikeouts);
output.println("HBP: " + hitsByPitch);
output.println("");
output.println("LEAGUE LEADERS");
output.println("BA: " + battingAverageLeader);
output.println("OB%: " + obpLeader);
output.println("H: " + hitsLeader);
output.println("BB: " + walksLeader);
output.println("K: " + strikeoutsLeader);
output.println("HPB: " + hitsByPitchLeader);
output.close();
}
}
}
我的输出
Pudge
BA: 0.450
OB: .500
H: 9
BB: 2
K: 8
HBP: 1
LEAGUE LEADERS
BA: Pudge
OB%: Pudge
H: Piazza
BB: Griffey
K: Pudge
HPB: Griffey
所需的输出示例
Mario
BA: .455
OB%: .563
H: 5
BB: 3
K: 2
HBP: 1
Luigi
BA: .364
OB%: .533
H: 4
BB: 2
K: 3
HBP: 2
Toad
BA: .600
OB%: .833
H: 3
BB: 7
K: 0
HBP: 0
LEAGUE LEADERS
BA: Toad
H: Mario
BB: Toad
K: Toad
HBP: Luigi
stats.txt
Griffey HHOHOKWOHKSPOOWSHHWWWWWWW
Piazza OOHHHKPSOHOOHWWHOSSSHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Pudge HHHHKOOHHHSWWHHOPKKKKKKK
答案 0 :(得分:0)
你在while循环的每次迭代中都覆盖了你的输出:
while (input.hasNextLine()) {
java.io.PrintWriter output = new java.io.PrintWriter("leaders.txt");
//Print LEAGUE LEADERS to leaders.txt
output.println(parts[0]);
output.printf("BA: %.3f\r\n", ba);
output.printf("OB: .%d\r\n", (int)(obp * accuracy) );
output.println("H: " + hits);
output.println("BB: " + walks);
output.println("K: " + strikeouts);
output.println("HBP: " + hitsByPitch);
output.println("");
output.println("LEAGUE LEADERS");
output.println("BA: " + battingAverageLeader);
output.println("OB%: " + obpLeader);
output.println("H: " + hitsLeader);
output.println("BB: " + walksLeader);
output.println("K: " + strikeoutsLeader);
output.println("HPB: " + hitsByPitchLeader);
output.close();
}
尝试这样的事情。
// initialize this *outside* the while loop
java.io.PrintWriter output = new java.io.PrintWriter("leaders.txt");
while (input.hasNextLine()) {
// Do the things...
// ...
// append output to the file.
output.println(parts[0]);
output.printf("BA: %.3f\r\n", ba);
output.printf("OB: .%d\r\n", (int)(obp * accuracy) );
output.println("H: " + hits);
output.println("BB: " + walks);
output.println("K: " + strikeouts);
output.println("HBP: " + hitsByPitch);
output.println("");
}
// *Now* print league leaders.
output.println("LEAGUE LEADERS");
output.println("BA: " + battingAverageLeader);
output.println("OB%: " + obpLeader);
output.println("H: " + hitsLeader);
output.println("BB: " + walksLeader);
output.println("K: " + strikeoutsLeader);
output.println("HPB: " + hitsByPitchLeader);
output.close(); // Good to close the file. :)