2013-03-11 71 views
0

我正在編寫一個菜單驅動的程序,允許用戶創建一定數量的團隊,然後記錄這些團隊的勝利和損失。我想格式化我的輸出,所以看起來很乾淨,但是我這樣做有點麻煩。我如何創建一個勝/負列?在java中格式化數組值

import java.util.Scanner; 

public class sports { 


public static void main(String[] args) { 


    System.out.println("Howdy sports fan!"); 

    String menuSelect; 
    String winSelect; 
    String loseSelect; 
    int teamSize = 0; 

    String[] teamsArray = new String[0]; 
    int[] winsArray = new int[0]; 
    int[] lossesArray = new int[0]; 


    do { 

     System.out.println("Please pick an option from the list below:"); 
     System.out.println("1) Create League"); 
     System.out.println("2) List all teams"); 
     System.out.println("3) Record a win");   
     System.out.println("4) Record a loss");   
     System.out.println("5) Quit");   
     Scanner keyboard = new Scanner(System.in); 
     menuSelect = keyboard.nextLine(); 

     if (menuSelect.equals("1")) 
     { 

      System.out.println("How many teams should I make?"); 
      teamSize = Integer.parseInt(keyboard.nextLine()); 
      teamsArray = new String[teamSize]; 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       System.out.println("Team " + (i+1) + "'s name?"); 
       teamsArray[i] = keyboard.nextLine();     
      } 
     } 

     else if (menuSelect.equals("2")) 
     { 

      System.out.printf("%15s %16s %n", "W", "L"); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       System.out.println(teamsArray[i]); 
       System.out.printf("%15d", winsArray[i]); 
       System.out.printf("%16d", lossesArray[i]); 

      } 
     } 

     else if (menuSelect.equals("3")) 
     { 
      winsArray = new int[teamSize]; 
      System.out.println("Which team won a game?"); 
      winSelect = keyboard.nextLine(); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       if (winSelect.equals(teamsArray[i])) 
       { 
        winsArray[i]++; 
       } 
      } 
     } 

     else if (menuSelect.equals("4")) 
     { 
      lossesArray = new int[teamSize]; 
      System.out.println("Which team lost a game?"); 
      loseSelect = keyboard.nextLine(); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       if (loseSelect.equals(teamsArray[i])) 
       { 
        lossesArray[i]++; 
       } 
      } 
     } 



    } while(!menuSelect.equals("5")); 

} 

} 
+0

什麼問題?也許你需要添加'\ n'來添加一箇中斷線。 – 2013-03-11 04:28:23

+2

您已經發布了*方式*太多的代碼。大部分與你的問題無關。請創建[一個簡短的,自包含的,正確的(可編譯的)示例](http://sscce.org/)。 – ruakh 2013-03-11 04:28:51

+1

代碼很長,但問題很簡單:-) – ktm5124 2013-03-11 04:41:29

回答

0

使用System.out.format。您可以設置字段長度,如 System.out.format(「%50s%50s」,string1,string2);

0

您可以使用System.out.printfString.format方法。請注意,我們使用的是「 - 」標誌設置的20寬見description.

public class Example { 
    public static void main(String[] args) { 
    String[] teams = {"Bobcats", "Tigers", "Lions", "Cheetahs", "Jackals", "Leopards", "Snow Leopards", "Cougars", "Mountain Lions", "Bobcats"}; 
    System.out.printf("%-30s %-20s %-20s %n", "Team Name", "No. of Wins", "No. of Losses"); 
    for (int i = 0; i < 10; i++) { 
     System.out.printf("%-30s %-20d %-20d %n", teams[i], i, i); 
    } 
    } 
} 

$ javac Example.java 
$ java Example 

Team Name      No. of Wins   No. of Losses   
Bobcats      0     0      
Tigers       1     1      
Lions       2     2      
Cheetahs      3     3      
Jackals      4     4      
Leopards      5     5      
Snow Leopards     6     6      
Cougars      7     7      
Mountain Lions     8     8      
Bobcats      9     9 

,很酷吧? :-)

+0

是的,這很好地工作,我怎麼會將球隊名稱添加到勝利專欄的左側?對不起,這個問題只是讓我感到困惑 – user2150807 2013-03-11 04:46:20

+0

沒問題。我會更新它來向你展示。您只需使用另一個轉換說明符:%s作爲字符串。 – ktm5124 2013-03-11 05:30:31

+0

重要的是你學習了轉換說明符(%s和%d)和「 - 」標誌來設置寬度(30爲團隊名稱,20爲勝/損失)。 – ktm5124 2013-03-11 05:37:45