有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要数组列表的指导

我该如何让我的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;
}

}

共 (1) 个答案

  1. # 1 楼答案

    变量“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;
    }
    
    }