商城库存清单小案例
1、需求:打印

2、步骤分析:
(1)实现表头,是固定数据,直接输出语句
(2)表格中间,商品数据,采用变量形式,定义变量,找对数据类型
输出所有变量
(3)表格尾巴,一部分数据固定
另一部分,商品数据进行数学计算,运算符
3、代码
1 public class Shopp{
2 public static void main(String args[]){
3 //输出表头固定数据
4 System.out.println("----------商场库存清单----------");
5 System.out.println("品牌型号 尺寸 价格 库存数");
6 //定义表格中的数据变量
7 //品牌型号 String;尺寸,价格 double;库存 int
8 String macBrand="MacBookAir";
9 double macSize=13.3;
10 double macPrice=6988.88;
11 int macCount=5;
12
13 String thinkBrand="ThinkPadT450";
14 double thinkSize=14;
15 double thinkPrice=5999.99;
16 int thinkCount=10;
17
18 String asusBrand="ASUS-FL5800";
19 double asusSize=15.6;
20 double asusPrice=4999.5;
21 int asusCount=18;
22 //商品价格信息变量进行打印,变量之间加入一定的字符串空格
23 System.out.println(macBrand+" "+macSize+" "+macPrice+" "+macCount);
24 System.out.println(thinkBrand+" "+thinkSize+" "+thinkPrice+" "+thinkCount);
25 System.out.println(asusBrand+" "+asusSize+" "+asusPrice+" "+asusCount);
26
27 //计算库存总数,所有商品数量库存求和
28 int totalCount=macCount+thinkCount+asusCount;
29 //计算所有商品库存的中金额,每个商品价格*库存数
30 double totalMoney=macCount*macPrice+thinkCount*thinkPrice+asusCount*asusPrice;
31 //输出表格底部
32 System.out.println("总库存数:"+totalCount);
33 System.out.println("商品库存总金额:"+totalMoney);
34 }
35 }
4、运行结果:



浙公网安备 33010602011771号