首页 > 解决方案 > 使用二维数组显示由方法填充的数据

问题描述

我正在尝试使用二维数组来存储投票数据,并使用一维数组来存储候选人姓名、细分名称,并保存候选人和替补的总票数。从主要方法中,我需要使用初始化列表初始化这些数组:包含所有投票数据(但不是总数)、候选人姓名、细分名称的二维数组,然后计算每个候选人和细分的总票数,然后打印出结果

我已经开始创建初始化和填充数组所需的方法,但我坚持要做才能继续

{
   private static int[][] Data;
   private static String[] names;
   private static String[] SubDivs;
   private static int[] totalvoteCand;
   private static int[] totalvoteSub;


   // method to initialize data array
   public static void InitData()
   {  
      int[][] tempData = {{600, 800, 800, 800},
         {700, 700, 700, 900},
         {800, 700, 800, 700},
         {400, 450, 300, 1300},
      {900, 900, 900, 1000}};
      Data = tempData;

      String[] tempNames = { {Audrey, Brian, Elizabeth, Peter, Zachary} };
      tempNames = names;

      String[] tempSub = { {Aberdeen, Brock, Sahali, Valleyview} };
      tempSub = SubDivs;
   }// end of init


   //*************************************************************//
   public static void calcCandVotes() // calculates candidate votes
   {
      totalvoteCand = new int[data[0].length];
      int tempTotal;

      for(int i = 0; i<data.length;i++) {
         tempTotal = 0;
         for(x = 0; x<data[0].length;x++){
            tempTotal += tempTotal + data[i][x];
         }
         totalVoteCand[i] = tempTotal;
      }
   }//end of calc cand

   //*************************************************************//
   public static void calcSubVotes()  // calculates subdivision votes
   {
      totalvoteSub = new int[data[0].length];
      int tempTotal;

      for(int i = 0; i<data[0].length; i++) {
         tempTotal = 0;
         for(x = 0;  x<data.length; x++){
            tempTotal += tempTotal + data[x][i];
         }
         totalvoteSub[x] = tempTotal;
      }
   }// end of calc sub ******************************************//

   public static void printArray() // print method, hopefully.
   {
      for(int i = 0; i<data.length;i++)
      {
         for (int x=0; x<data[0].length;x++)
            System.out.print(Data[i][x] + "\t");
         System.out.println();
      }
   }
   //***************************end of print array****************************************

标签: java

解决方案


你的意思是你想这样打印?

int[][] votes = {{600, 800, 800,  800},
                 {700, 700, 700,  900},
                 {800, 700, 800,  700},
                 {400, 450, 300, 1300},
                 {900, 900, 900, 1000}};
String[] names = {"Audrey", "Brian", "Elizabeth", "Peter", "Zachary"};
String[] subs = {"Aberdeen", "Brock", "Sahali", "Valleyview"};
System.out.printf("%-10s ", "Candidate");
for (int col = 0; col < subs.length; col++) {
    System.out.printf("%10s ", subs[col]);
}
System.out.printf("%10s%n", "Total");

for (int row = 0; row < names.length; row++) {
    int total = 0;
    System.out.printf("%-10s ", names[row]);
    for (int col = 0; col < subs.length; col++) {
        total += votes[row][col];
        System.out.printf("%10d ", votes[row][col]);
    }
    System.out.printf("%10d%n", total);
}

int grandTotal = 0;
System.out.printf("%-10s ", "Total");
for (int col = 0; col < subs.length; col++) {
    int total = 0;
    for (int row = 0; row < names.length; row++) {
        total += votes[row][col];
    }
    grandTotal += total;
    System.out.printf("%10d ", total);
}
System.out.printf("%10d%n", grandTotal);

输出

Candidate    Aberdeen      Brock     Sahali Valleyview      Total
Audrey            600        800        800        800       3000
Brian             700        700        700        900       3000
Elizabeth         800        700        800        700       3000
Peter             400        450        300       1300       2450
Zachary           900        900        900       1000       3700
Total            3400       3550       3500       4700      15150

推荐阅读