【问题标题】:how to correctly use formatter on a 2 dimensional array?如何在二维数组上正确使用格式化程序?
【发布时间】:2012-10-09 17:11:01
【问题描述】:

我必须将一列中二维数组的行之和添加到右侧。但是,当我运行代码时,它会添加每列中所有行的总和,直到它到达末尾。像这样:http://i.imgur.com/7CCPu.png 总和是正确的,但假设每行只有一次。

      public static void main(String[] args) throws IOException
{
    // TODO code application logic here
    File election = new File("voting_2008.txt");
    Scanner sc = new Scanner(election);

    String[] states = new String[51];
    int[][]votes = new int[51][4];
    int[] Totalbystate = new int[votes.length];

    for (int s=0; s < 51; s++)
    {
        states[s] = sc.nextLine();
    }

    for(int c=0; c < 3; c++)
    {
        for(int s=0; s < 51; s++)
        {
            votes[s][c] = sc.nextInt();

        }

    }
    Formatter fmt = new Formatter();
    fmt.format("%20s%12s%12s%12s%21s", "State", "Obama", "McCain", "Other", "Total by state");
    System.out.println(fmt);
    for (int s=0; s < 51; s++)
    {
       fmt = new Formatter();
       fmt.format("%20s", states[s]);
       System.out.print(fmt);
       for(int c=0; c < 3; c++)
       {
           fmt = new Formatter();
           fmt.format("%12d", votes[s][c]);
           System.out.print(fmt);

       }

       for(int row=0; row < votes.length; row++)
       {   
            int sum =0;
           for (int col=0; col < votes[row].length; col++)
           {
              sum = sum + votes[row][col];

           }
          fmt = new Formatter();
            fmt.format("%21d", sum);
            System.out.print(fmt);  
       }


       System.out.println();
    }
}

}

【问题讨论】:

  • 你又来了同样的问题??你本可以只在那里要求澄清。事实上你在那里接受了答案。
  • 抱歉,一位用户告诉我这是一个不同的问题
  • @user1663414.. 查看我的更新以解决您的代码中的问题.. 我建议了一个适合您的更改.. :)

标签: java multidimensional-array formatter


【解决方案1】:
   for(int row=0; row < votes.length; row++)
   {   
        int sum =0;
        for (int col=0; col < votes[row].length; col++)
         {
          sum = sum + votes[row][col];

        }
        fmt = new Formatter();
        fmt.format("%21d", sum);  
        System.out.print(fmt); // You are not printing newline after each vote..
   }

尝试使用 : - System.out.println(fmt); 代替注释代码..

更新 :- 不要进行上述更改.. 将不起作用.. 确实进行以下更改。

这就是问题所在。您正在打印您的total no of votes,51 * 51 次。您在另一个循环中拥有这个循环,该循环已经运行了 51 次......

所以,解决您的问题..从上面的代码中删除外循环,并保持如下:-

   //for(int row=0; row < votes.length; row++) // Remove this loop
   //{ // Remove this

        int sum =0;
        for (int col=0; col < votes[s].length; col++)
         {
          sum = sum + votes[s][col];

        }
        fmt = new Formatter();
        fmt.format("%21d", sum);  
        System.out.print(fmt);
   //} // Remove this also..

s 的值是您从最外层​​循环中获得的 ..

更新: - 如果您想要所有投票的总和.. 对于所有列..

int totalSum = 0;    // Declare a variable outside it..
for (int s=0; s < 51; s++)   // This is the place where the loop started
{
   fmt = new Formatter();
   fmt.format("%20s", states[s]);
   System.out.print(fmt);

   // Other inner loops

    int sum =0;   // Your innerloop that calculates sum of each state..
    for (int col=0; col < votes[s].length; col++)
     {
      sum = sum + votes[s][col];

    }

    totalSum += sum;   // Add sum to `totalSum`
    fmt = new Formatter();
    fmt.format("%21d", sum);  
    System.out.print(fmt);

    // Continue with the outer loop
}

【讨论】:

  • 当我删除了你提到的循环时,变量行消失了,这给了我“找不到符号”“变量行”错误。谢谢。
  • @user1663414.. 哦。对不起..我忘了提..用s替换row..最外层循环的变量
  • 我想知道如果我也想对列求和怎么办?你会怎么做?谢谢...
  • @user1663414.. 在这种情况下,在最外面的循环之外有另一个变量totalSum:-for(s = 0; s &lt; 51; s++),然后将您获得的总和添加到那个.. 在您打印的地方之后sum..
  • @user1663414.. 查看我的最新更新.. 我采用了您的部分代码。您需要通过匹配循环将其安装在适当的位置..
猜你喜欢
  • 2012-09-30
  • 2021-04-24
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2017-03-14
相关资源
最近更新 更多