输出效果
代码:
1 package com.oracle.Shopping; 2 //修饰符+class+类名 3 public class Shopping { 4 //修饰符 + 返回值 +主方法(){} 5 public static void main(String[] args) { 6 //打印方法 7 System.out.println("--------------------商城库存--------------------"); 8 //一维数组,静态初始化 9 String[] list = {"品牌型号","尺寸","价格","库存"};//静态初始化 10 String[] brand = new String[]{"MacBookAir","ThinkpadT450","ASUS-FL5800"}; 11 //String[] brand = {"MacBookAir","ThinkpadT450","ASUS-FL5800"}; 12 //String[] brand = new String[3];//动态初始化,然后赋值 13 double[] size = new double[3]; 14 size[0]=13.3; 15 size[1]=14.0; 16 size[2]=15.6; 17 double[] money = {6988.88,5999.99,4999.5}; 18 int[] repertory={5,10,18}; 19 System.out.print(list[0]+" "+" "+list[1]+" "+list[2]+" "+list[3]+" "); 20 System.out.println(); 21 //需要确保一维数组的下标一致(下标不一致暂时是重新打印的) 22 int sum = 0;//设置总库存数初始值为0 23 double amount = 0;//设置总金额初始值为0 24 //for遍历 25 for(int i=0;i<brand.length;++i){ 26 //打印方法, 表示Tab键 27 System.out.println(brand[i]+" "+size[i]+" "+money[i]+" "+repertory[i]+" "); 28 //计算库存数 29 sum = sum+ repertory[i]; 30 //计算总金额 31 amount =amount+ money[i]*repertory[i]; 32 } 33 System.out.println("------------------------------------------------"); 34 System.out.println("总库存数:"+sum); 35 System.out.println("库存商品总金额:"+amount); 36 } 37 }
java