Java 打印在for循环中填充的数组
我今天带着一个非常基本的任务来这里,不知怎么搞的,我真的很困惑。 我有一个数组,看起来像这样: 代码如下:Java 打印在for循环中填充的数组,java,arrays,for-loop,Java,Arrays,For Loop,我今天带着一个非常基本的任务来这里,不知怎么搞的,我真的很困惑。 我有一个数组,看起来像这样: 代码如下: double population[][] = {{281.0, 296.0, 325.0, 371.0, 384.5}, {298.6, 241.2, 301.2, 342.8, 388.7}, {362.9, 284.1, 276.8, 353.6, 395.1}, {393.4, 344.8, 295.6, 298.3, 375.0}}; int
double population[][] = {{281.0, 296.0, 325.0, 371.0, 384.5},
{298.6, 241.2, 301.2, 342.8, 388.7},
{362.9, 284.1, 276.8, 353.6, 395.1},
{393.4, 344.8, 295.6, 298.3, 375.0}};
int year[] = {2011, 2016, 2021, 2026, 2031};
String ageGroup[] = {"15-19", "20-24", "25-29", "30-34",};
String output = "Actual and Projected Population in thousands by Age Group (CSO 2016)";
output += String.format("\n%10s", "");
for (int i = 0; i < year.length; i++) {
output += String.format("%10s", year[i]);
}
output += String.format("%10s", "%Change");
double change[] = new double[ageGroup.length];
for (int i = 0; i < population.length; i++) {
output += String.format("\n%10s ", ageGroup[i]);
for (int j = 0; j < population[i].length; j++) {
output += String.format("%10.1f", population[i][j]);
}
change[i] = (((population[i][4] - population[i][0])/
population[i][0]) * 100);
output += String.format("%10.1f", change[i]);
}
output += String.format("\n\nTotal (15 - 34): ");
System.out.println(output);
}
我也试着在这里添加它
for (int i = 0; i < population.length; i++) {
output += String.format("\n%10s ", ageGroup[i]);
for (int j = 0; j < population[i].length; j++) {
output += String.format("%10.1f", population[i][j]);
}
change[i] = (((population[i][4] - population[i][0])/
population[i][0]) * 100);
output += String.format("%10.1f", change[i]);
total[i] = (population[0][i] + population[1][i]+ population[2][i]+population[3][i]+population[4][i]);
}
for(int i=0;i
现在,我感到困惑的是,这么简单的任务怎么会让我如此困难。双重人口[]={{281.0,296.0,325.0,371.0,384.5},{298.6241.2,301.2,342.8,388.7},
double population[][] = { { 281.0, 296.0, 325.0, 371.0, 384.5 }, { 298.6, 241.2, 301.2, 342.8, 388.7 },
{ 362.9, 284.1, 276.8, 353.6, 395.1 }, { 393.4, 344.8, 295.6, 298.3, 375.0 } };
int year[] = { 2011, 2016, 2021, 2026, 2031 };
String ageGroup[] = { "15-19", "20-24", "25-29", "30-34", };
String output = "Actual and Projected Population in thousands by Age Group (CSO 2016)";
output += String.format("\n%10s", "");
for (int i = 0; i < year.length; i++) {
output += String.format("%10s", year[i]);
}
output += String.format("%10s", "%Change");
double change[] = new double[ageGroup.length];
for (int i = 0; i < population.length; i++) {
output += String.format("\n%10s ", ageGroup[i]);
for (int j = 0; j < population[i].length; j++) {
output += String.format("%10.1f", population[i][j]);
}
change[i] = (((population[i][4] - population[i][0]) / population[i][0]) * 100);
output += String.format("%10.1f", change[i]);
}
output += String.format("\n\nTotal (15 - 34): ");
// here i changed the code
// this loop will print the last from (15 to 35)
// the outer loop iterate like 2011 2016 and so one
for (int i = 0; i < 5; i++) {
double temp = 0;
// the inner loop iterate from up to down and add values
for (int j = 0; j < 4; j++) {
temp = temp + population[j][i];
}
output += String.format("%10.1f", temp);
}
System.out.println(output);
{ 362.9, 284.1, 276.8, 353.6, 395.1 }, { 393.4, 344.8, 295.6, 298.3, 375.0 } };
国际年[]={2011、2016、2021、2026、2031};
字符串年龄组[]={“15-19”、“20-24”、“25-29”、“30-34”、};
字符串输出=“按年龄组划分的千人实际和预计人口(CSO 2016)”;
输出+=String.format(“\n%10s”,”);
对于(int i=0;i
如果讲师不把面向对象的思想传授给学生,迫使学生在数组中混淆视听,而不是对领域建模,这是一种犯罪。唉
我想这里有两把钥匙。首先,将计算与表示分开。第二,认识到行是年龄组,列是年。真的应该有方法来处理这些东西,而不仅仅是在一个main
方法中。此外,标题和一些绒毛可以在最终输出中固定
此外,与硬编码(例如,[4]
)不同,此代码使用数组的.length
使添加另一个年份或另一个年龄组变得更容易
//
// a 2d array, where row is for a given agent group, and column
// is for a given year
//
static double population[][] = { { 281.0, 296.0, 325.0, 371.0, 384.5 },
{ 298.6, 241.2, 301.2, 342.8, 388.7 },
{ 362.9, 284.1, 276.8, 353.6, 395.1 },
{ 393.4, 344.8, 295.6, 298.3, 375.0 } };
static int year[] = { 2011, 2016, 2021, 2026, 2031 };
static String ageGroup[] = { "15-19", "20-24", "25-29", "30-34", };
public static void main(String[] args)
{
//
// hold the totals
//
double[] yearTot = new double[year.length]; // total by year
double[] agTot = new double[ageGroup.length]; //total by ag
double[] chngAG = new double[ageGroup.length]; //change
// loop over every age group
for (int ag = 0; ag < ageGroup.length; ++ag) {
// get the population for the age group, which is
// one row in the data
double[] valsForAG = population[ag];
// loop over every year, which a column in a given age group
for (int yr = 0; yr < year.length; ++yr) {
// get the specific value
double valForAgInYear = valsForAG[yr];
// add to the total for the year and to the age group value
yearTot[yr] += valForAgInYear;
agTot[ag] += valForAgInYear;
} // for every year
int en = ageGroup.length;
int st = 0;
// after processing an age group, calculate the change
chngAG[ag] = ( ( (valsForAG[en] - valsForAG[st]) /
valsForAG[st]) * 100);
} // for every age group
//
// do the output
//
// header row
System.out.printf("%10s", "");
for (int y = 0; y < year.length; ++y) {
System.out.printf("\t%7d", year[y]);
}
System.out.printf("\t%10s%n", "%Change");
// data
for (int ag = 0; ag < ageGroup.length; ++ag) {
System.out.printf("%10s", ageGroup[ag]);
for (int yr = 0; yr < year.length; ++yr) {
System.out.printf("\t%7.1f", population[ag][yr]);
}
System.out.printf("%10.1f", chngAG[ag]);
System.out.println();
}
//output the totals
System.out.println();
System.out.printf("%10s", "Totals:");
for (int t = 0; t < yearTot.length; ++t) {
System.out.printf("\t%7.1f", yearTot[t]);
}
System.out.println();
}
总计:1335.9 1166.1 1198.6 1365.7 1543.3
publicstaticvoidmain(字符串[]args){
双重人口[][]={{281.0,296.0,325.0,371.0,384.5},{298.6,241.2,301.2,342.8,388.7},{362.9,284.1,276.8,353.6,395.1},{393.4,344.8,295.6,298.3,375.0};
//1.步骤:查找数组中最长的
//如果阵列的长度不同,则需要此选项,例如:
/*
双种群[][]={
{281.0, 296.0, 325.0, 371.0, 384.5},
{298.6, 241.2, 301.2, 342.8, 388.7, 0},
{362.9, 284.1, 276.8, 353.6, 395.1, 1, 2},
{393.4, 344.8, 295.6, 298.3, 375.0, 0.5}
};
*/
int lengthoflongestaray=总体[0]。长度;
对于(int i=0;i长度{
LengthoFlongeStaray=总体[i]。长度;
}
}
//2.步骤:计算总和
双倍结果[]=新的双倍[LengthoflongeStaray];
对于(int i=0;i
说明:
为了避免混淆循环和奇特的逻辑,我创建了一个数组,它将保存名为result的计算结果
A.Swing标记已删除—这个问题与Swing GUI编程有什么关系?首先,我将收集数据,然后创建输出—将数据与演示分离。第二,您是否被限制使用数组而不是实际的域对象?并且您不需要单独的for循环来创建总计。可以在当前嵌套for循环之前创建一个总计数组,并在嵌套for循环中增加该数组中已有的值。你只需要小心你使用的索引。把它写在纸上。我犯了一个错误,就是挥杆太熟练了。我已经有一段时间没有使用GUI了。我编辑了我的帖子,向你展示了我是如何在已有的循环中填充数组的。@KevinO我必须使用数组。你能解释一下你的答案吗?仅仅转储代码并没有多大帮助。请同意@pzaenger。答案转储对任何人都没有帮助。@pzaenger亲爱的,请再次检查:)感谢您为代码提供了像样的解释。非常棒的解释!非常感谢。
//
// a 2d array, where row is for a given agent group, and column
// is for a given year
//
static double population[][] = { { 281.0, 296.0, 325.0, 371.0, 384.5 },
{ 298.6, 241.2, 301.2, 342.8, 388.7 },
{ 362.9, 284.1, 276.8, 353.6, 395.1 },
{ 393.4, 344.8, 295.6, 298.3, 375.0 } };
static int year[] = { 2011, 2016, 2021, 2026, 2031 };
static String ageGroup[] = { "15-19", "20-24", "25-29", "30-34", };
public static void main(String[] args)
{
//
// hold the totals
//
double[] yearTot = new double[year.length]; // total by year
double[] agTot = new double[ageGroup.length]; //total by ag
double[] chngAG = new double[ageGroup.length]; //change
// loop over every age group
for (int ag = 0; ag < ageGroup.length; ++ag) {
// get the population for the age group, which is
// one row in the data
double[] valsForAG = population[ag];
// loop over every year, which a column in a given age group
for (int yr = 0; yr < year.length; ++yr) {
// get the specific value
double valForAgInYear = valsForAG[yr];
// add to the total for the year and to the age group value
yearTot[yr] += valForAgInYear;
agTot[ag] += valForAgInYear;
} // for every year
int en = ageGroup.length;
int st = 0;
// after processing an age group, calculate the change
chngAG[ag] = ( ( (valsForAG[en] - valsForAG[st]) /
valsForAG[st]) * 100);
} // for every age group
//
// do the output
//
// header row
System.out.printf("%10s", "");
for (int y = 0; y < year.length; ++y) {
System.out.printf("\t%7d", year[y]);
}
System.out.printf("\t%10s%n", "%Change");
// data
for (int ag = 0; ag < ageGroup.length; ++ag) {
System.out.printf("%10s", ageGroup[ag]);
for (int yr = 0; yr < year.length; ++yr) {
System.out.printf("\t%7.1f", population[ag][yr]);
}
System.out.printf("%10.1f", chngAG[ag]);
System.out.println();
}
//output the totals
System.out.println();
System.out.printf("%10s", "Totals:");
for (int t = 0; t < yearTot.length; ++t) {
System.out.printf("\t%7.1f", yearTot[t]);
}
System.out.println();
}
2011 2016 2021 2026 2031 %Change
15-19 281.0 296.0 325.0 371.0 384.5 36.8
20-24 298.6 241.2 301.2 342.8 388.7 30.2
25-29 362.9 284.1 276.8 353.6 395.1 8.9
30-34 393.4 344.8 295.6 298.3 375.0 -4.7
public static void main(String[] args) {
double population[][] = {{281.0, 296.0, 325.0, 371.0, 384.5}, {298.6, 241.2, 301.2, 342.8, 388.7}, {362.9, 284.1, 276.8, 353.6, 395.1}, {393.4, 344.8, 295.6, 298.3, 375.0}};
//1. Step: Find the longest of the arrays
//You need this if the length of the arrays is different, for example:
/*
double population[][] = {
{281.0, 296.0, 325.0, 371.0, 384.5},
{298.6, 241.2, 301.2, 342.8, 388.7, 0},
{362.9, 284.1, 276.8, 353.6, 395.1, 1, 2},
{393.4, 344.8, 295.6, 298.3, 375.0, 0.5}
};
*/
int lengthOfLongestArray = population[0].length;
for(int i = 0; i < population.length; i++){
if(population[i].length > lengthOfLongestArray){
lengthOfLongestArray = population[i].length;
}
}
//2. Step: calculate the sum
double result[] = new double[lengthOfLongestArray];
for(int i = 0; i < population.length; i++){
for(int j = 0; j < population[i].length; j++){
result[j] += population[i][j];
}
}
System.out.println(Arrays.toString(result));
}