需求1:
求五个学生的分数的平均分
public class Demo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int score[] = new int[5];
int sum = 0;
Scanner input = new Scanner(System.in);
int length = score.length;
for (int i = 0; i < length; i++) {
score[i] = input.nextInt();
sum += score[i];
}
double arg = (double) sum / length;
System.out.print("平均分为" + arg + "分");
}
}
需求2:
import java.util.Scanner;
public class Demo04 {
public static void main(String[] arge) {
int price[] = new int[101];
Scanner input = new Scanner(System.in);
int sum = 0;
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "笔金额:");
price[i] = input.nextInt();
sum += price[i];
}
System.out.println("序号" + "\t" + "金额");
for (int i = 0; i < 5; i++) {
System.out.println((i + 1) + "\t" + price[i]);
}
}
}