随笔

JAVA学习网 2019-03-10 14:42:02

import java.util.Scanner;
public class LianXi6{
 public static void main(String[] args){
  double total = 0;
  //创建一个长度为5的数组
  double[] price = new double[5];
  //导入  Scanner  类  键盘输入
  Scanner sc = new Scanner(System.in);
  for(int i=0;i<price.length;i++){
   System.out.print("请输入第"+(i+1)+"笔购物的金额:");
   // 通过Scanner类 接受键盘输入的内容
    price[i] = sc.nextDouble();
    // 通过for循环  数组中的元素累加
    total += price[i];
  }
  System.out.println("序号\t\t价格(元)");
  for(int i=0;i<price.length;i++){
   System.out.println((i+1)+"\t\t"+price[i]);
  }
  System.out.println("总价为\t\t"+total);
 }
}

阅读(2444) 评论(0)