2014-12-08 138 views
1

我有点新的节目和我的工作在学校作业上的阵列阵列显示垂直而非水平

我想编写出使用数组,存储的统计数据的程序。

import java.io.*; 
public class HockeyLeague { 
static final int Rows = 7; 
static final int Cols = 8; 
static double HockeyChart [][] = new double[Rows][Cols]; 
static HockeyLeague HL = new HockeyLeague(); 
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
public static void main(String args[])throws IOException { 
    while(true){ 
     System.out.println("Welcome to the NHL statistic program"); 
     System.out.println("Would you like to proceed to the program? <y for yes, n for no>"); 
     final String menuDecision = br.readLine(); 
     if (menuDecision.equals("n")) { 
      System.out.println("Goodbye"); 
      System.exit(0); 
     } 
     if (menuDecision.equals("y")) { 
      while(true) { 
      System.out.println("The 8 teams are Toronto Maple Leafs, Montreal Canadiens, Ottawa Senators, Detroit Red Wings, Boston Bruins,"+ 
      " Chicago Blackhawks, New York Islanders, and Pitsburg Penguins"); 
      System.out.println("To input statistics for Toronto, enter '0' "); 
      System.out.println("To input statistics for Montreal, enter '1' "); 
      System.out.println("To input statistics for Ottawa, enter '2' "); 
      System.out.println("To input statistics for Detroit, enter '3' "); 
      System.out.println("To input statistics for Boston, enter '4' "); 
      System.out.println("To input statistics for Chicago, enter '5' "); 
      System.out.println("To input statistics for New York, enter '6' "); 
      System.out.println("To input statistics for Pitsburg, enter '7' "); 
      int numString = Integer.parseInt(br.readLine()); 

      Info (numString); 

      } 
     } 
    }  
} 

public static double[][] Info(int teamInput)throws IOException{ 
    System.out.println("Enter the amount of games played"); 
    int games = Integer.parseInt(br.readLine()); 
    HockeyChart [0+teamInput][1] = games; 
    System.out.println("Enter the amount of wins"); 
    int wins = Integer.parseInt(br.readLine()); 
    HockeyChart [0+teamInput][2] = wins; 
    System.out.println("Enter the amount of ties"); 
    int ties = Integer.parseInt(br.readLine()); 
    HockeyChart [0+teamInput][3] = ties; 
    System.out.println("Enter the amount of losses"); 
    int losses = Integer.parseInt(br.readLine()); 
    HockeyChart [0+teamInput][4] = losses; 
    System.out.println("Enter the amount of goals scored"); 
    int goals = Integer.parseInt(br.readLine()); 
    HockeyChart [0+teamInput][5] = goals; 
    for (int i = 0; i < Rows; i ++) { 
     for (int j = 0; j < Cols;j ++) { 
      System.out.println(HockeyChart[i][j] + " "); 
     } 
     System.out.println(" "); 
    } 
    return HockeyChart; 
} 
} 

这是我想出的程序。我不明白为什么我会得到一个很长的垂直行数的输出,而不是一排排的数字。

任何帮助,将不胜感激!谢谢

+1

除此之外,我强烈建议学习有关Java命名约定,并开始跟随他们 - 变量和方法应该以小写字母开头,而你'Info'方法应该更像'enterTeamInfo'。我强烈建议养成所有领域都是私有的习惯。 – 2014-12-08 06:54:39

回答

0

在你的Info方法中,当迭代数组时,你使用的是System.out.println(),而不是你需要使用System.out.print()方法。

for (int i = 0; i < Rows; i ++) { 
    for (int j = 0; j < Cols;j ++) { 
     System.out.print(HockeyChart[i][j] + " "); 
    } 
    System.out.println(); 
} 

第二个println语句将帮助您在打印一行后移动到下一行。

0

看看你的循环:

for (int j = 0; j < Cols;j ++) { 
    System.out.println(HockeyChart[i][j] + " "); 
} 

println是短期的打印线 - 它打印给定的字符串,然后移动到下一行。如果你要打印的阵列中的所有内容在一个单一的线,你可以使用System.out.print代替:

for (int j = 0; j < Cols;j ++) { 
    System.out.print(HockeyChart[i][j] + " "); 
} 
0

您正在使用System.out.println这意味着在新行打印。

您可以使用System.out.print作为相同的行,即水平行。

代码会是这样

for (int i = 0; i < Rows; i ++) 
{ 
    for (int j = 0; j < Cols;j ++) { 
     System.out.print(HockeyChart[i][j] + " "); 
    } 
    System.out.println(" "); 
} 
return HockeyChart; 
从别的