【发布时间】:2019-09-14 15:22:11
【问题描述】:
我正在尝试使用二维数组来存储投票数据,并使用一维数组来存储候选人姓名、分区名称,并保存候选人和替补的总票数。从主要方法中,我需要使用初始化列表初始化这些数组:包含所有投票数据(但不是总数)、候选人姓名、细分名称的二维数组,然后计算每个候选人和细分的总票数,然后打印出结果
我已经开始创建初始化和填充数组所需的方法,但我仍然坚持需要做些什么才能继续
{
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****************************************
【问题讨论】:
-
你确定
tempNames = names;?你不是说要分配其他方式吗?tempSub = SubDivs;也一样?
标签: java