如何让我的txt输出以下格式!它仅输出我输入的第一行,但希望它看起来像以下>>
这是我希望输出显示的示例 - http://i.stack.imgur.com/7Lfr0.png
import java.io.FileOutputStream;
import java.io.IOException;
import java.io[enter image description here][1].PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class GAMESCORE {
private static char[] input;
public static void main(String[] args) {
int[] minutesPlayed = new int [100];
String gamerName, gamerReport;
String[] gameNames = new String[100];
int[] highScores = new int[100];
@SuppressWarnings("resource")
Scanner Scan = new Scanner(System.in);
System.out.println("-------------- Game Score Report Generator --------------");
System.out.println(" ");
System.out.println("Enter Your Name");
gamerName = Scan.nextLine();
boolean isEmpty = gamerName == null || gamerName.trim().length() == 0;
if (isEmpty) {
System.out.print("Enter your Name.");
gamerName = Scan.nextLine();
}
System.out.println("Enter details in this format - " + " -->");
System.out.println(" ");
System.out.println("Game : Achievement Score : Minutes Played");
gamerReport = Scan.nextLine();
Scanner scanner = new Scanner(System.in);
List<String> al = new ArrayList<String>();
String word;
while (scanner.hasNextLine()) {
word = scanner.nextLine();
if (word != null) {
word = word.trim();
if (word.equalsIgnoreCase("quit")) {
break;
}
al.add(word);
} else {
break;
}
}
String[] splitUpReport;
splitUpReport = gamerReport.split(":");
int i = 0;
gameNames[i] = splitUpReport[0];
highScores[i] = Integer.parseInt(splitUpReport[1].trim() );
minutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());
try
{
PrintWriter writer = new PrintWriter(new FileOutputStream("Gaming Report Data.txt", true));
writer.println("Player : " + gamerName);
writer.println();
writer.println("--------------------------------");
writer.println();
String[] report = gamerReport.split(":");
writer.println("Game: " + report[0] + ", score= " +report[1] + ", minutes played= " +report[2]);
//writer.println("Games Played : " + minutesPlayed);
writer.close();
} catch (IOException e)
{
System.err.println("You have made an error with data input");
}
System.out.println("You have quit!");
}
public static char[] getInput() {
return input;
}
public static void setInput(char[] input) {
GAMESCORE.input = input;
}
}
答案 0 :(得分:0)
变量“gamerReport”是为第一行设置的,除了打印它之外永远不会被触摸,列表“al”添加了东西,但从未使用过。也许尝试这样的事情?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class GAMESCORE {
private static char[] input;
public static void main(String[] args) {
int totalGames, totalAchievements, totalTime;
totalGames = 0;
totalAchievements = 0;
totalTime = 0;
String gamerName, gamerReport;
@SuppressWarnings("resource")
Scanner Scan = new Scanner(System.in);
System.out.println("-------------- Game Score Report Generator --------------");
System.out.println(" ");
System.out.println("Enter Your Name");
gamerName = Scan.nextLine();
boolean isEmpty = gamerName == null || gamerName.trim().length() == 0;
if (isEmpty) {
System.out.print("Enter your Name.");
gamerName = Scan.nextLine();
}
System.out.println("Enter details in this format - " + " -->");
System.out.println(" ");
System.out.println("Game : Achievement Score : Minutes Played");
gamerReport = Scan.nextLine();
Scanner scanner = new Scanner(System.in);
List<String> al = new ArrayList<String>();
String word;
while (scanner.hasNextLine()) {
word = scanner.nextLine();
if (word != null) {
word = word.trim();
if (word.equalsIgnoreCase("quit")) {
break;
}
al.add(word);
} else {
break;
}
}
try
{
PrintWriter writer = new PrintWriter(new FileOutputStream("Gaming Report Data.txt", true));
writer.println("Player : " + gamerName);
writer.println();
writer.println("--------------------------------");
writer.println();
for(String listString : al){
String[] splitUpReport;
splitUpReport = listString.split(":");
writer.println("Game: " + splitUpReport[0].trim() + ", score= " + splitUpReport[1].trim() + ", minutes played= " +splitUpReport[2].trim());
totalGames++;
totalTime += Integer.parseInt(splitUpReport[2].trim());
totalAchievements += Integer.parseInt(splitUpReport[1].trim());
}
writer.println();
writer.println("--------------------------------");
writer.println();
writer.println("Games Played: " + String.valueOf(totalGames));
writer.println("Total Achievement: " + String.valueOf(totalAchievements));
writer.println("Total Time: " + String.valueOf(totalTime) + " (" + String.valueOf(totalTime/60) + " hours and " + String.valueOf(totalTime%60) + " minutes)");
//writer.println("Games Played : " + minutesPlayed);
writer.close();
} catch (IOException e)
{
System.err.println("You have made an error with data input");
}
System.out.println("You have quit!");
}
public static char[] getInput() {
return input;
}
public static void setInput(char[] input) {
GAMESCORE.input = input;
}
}