[code=Java][/code]
import java.util.Scanner;
public class settlement {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double total = 0;
double money[] = new double[4];
for(int i =0;i<=money.length;i++){
System.out.print("请输入第"+(i+1)+"笔购物的金额");
money[i]=input.nextDouble();
}
System.out.println("序号\t\t金额(元)");
int i=0;
for( ;i<=money.length;i++);{
System.out.println((i+1)+"\t\t"+money[4]);
total =total+money[4];
}
System.out.println("总金额\t\t"+total);
}
}
输出数组的值,然后再累加数组的值
------解决方案--------------------
数组越界的问题,贴出代码来如下
你声明4个元素,数组下标从0开始最大就到3的
- Java code
package csdn.p7;import java.util.Scanner;public class Settlement { public static void main(String[] args) { Scanner input = new Scanner(System.in); double total = 0; double money[] = new double[4]; for(int i =0;i<money.length;i++){ System.out.print("请输入第"+(i+1)+"笔购物的金额"); money[i]=input.nextDouble(); } System.out.println("序号\t\t金额(元)"); for(int i=0;i<money.length;i++){ System.out.println(i+""+money[i]); total=total+money[i]; } System.out.println("总金额\t\t"+total); }}
------解决方案--------------------
new double[5];这个数字你声明几个数组就是多大!!