gpt4 book ai didi

java - 使用二维数组显示由方法填充的数据

转载 作者:行者123 更新时间:2023-12-02 09:31:26 25 4
gpt4 key购买 nike

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

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

{
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****************************************

最佳答案

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

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

关于java - 使用二维数组显示由方法填充的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com