在eclipse上写程序输出数组,结果for(double b : row) System.out.print(b);的输出方式有问题,不能运行。请问怎样才能用这种方式?
在源代码的第38、40行
public class CompoundInterest
{
public static void main(String[] args)
{
// TODO 自动生成的方法存根
final double STARTRATE = 10;
final int NRATES = 6;
final int NYEARS = 10;
double[] interestRate = new double[NRATES];
for(int j = 0; j < interestRate.length; j++)
interestRate[j] = (STARTRATE + j) / 100.0;
double[][] balances = new double[NYEARS][NRATES];
for(int j = 0; j < balances[0].length; j++)
balance[0][j] = 10000;
for(int i = 1; i < balances.length; i++)
{
for(int j = 0; i < balances[i].length; j++)
{
double oldBalance = balances[i - 1][j];
double interest = oldBalance * interestRate[j];
balances[i][j] = oldBalance + interest;
}
}
for(int j = 0;j < interestRate.length; j++)
System.out.print(100 * interestRate[j]);
System.out.println();
for(double[] row : balances)
{
for(double b : row)
System.out.print(b);
System.out.println();
}
}
}
非常感谢!
------解决思路----------------------
确定第38、40行报错吗?
18行和22行有写错吧。。
------解决思路----------------------
double[][] balances 是二位数组,,你用for(double[] row : balances)一维数组遍历,肯定不对啊