7.java练习=超市收银

package com.cl.test;

import java.util.Scanner;

public class Tset10 {

	public static void main(String[] args) {
		// 超市收银
		Scanner cl=new Scanner(System.in);
		
		String name1="苹果";
		double JG1=3.9;
		String BH1="1001";
		
		String name2="香蕉";
		double JG2=5.9;
		String BH2="1002";
		
		String name3="梨子";
		double JG3=9.9;
		String BH3="1003";
		
		System.out.println("欢迎来到超市收银系统!");
		System.out.println("请输入商品编号");
		String num=cl.next();
		System.out.println("请输入商品数量");
		int count=cl.nextInt();
		
		String name="";
		double JG=0;
		
		if (num.equals(BH1)) {
			name=name1;
			JG=JG1;
		}else if (num.equals(BH2)) {
			name=name2;
			JG=JG2;
		}else if (num.equals(BH3)) {
			name=name3;
			JG=JG3;
		}else {
			System.out.println("查无此商品");
		}
		
		double money=JG*count;

		System.out.println("您购买的是:"+name+" 数量:"+count+" 总价格:"+money);
		
		System.out.println("请输入付款金额");
		double JE=cl.nextDouble();
		
		if (JE<money) {
			System.out.println("金额不足");
			return;
		}else {
			System.out.println("您购买了"+count+"个"+name+" 总价格:"+money+  "你的付款金额:"+JE+"  找零:"+(JE-money)+"元");
			System.out.println("谢谢惠顾,欢迎下次光临");
		}
		
		
		
		

	}

}

下面为显示小数点位数的例子:

package com.cl.test;

import java.text.DecimalFormat;

public class tset11 {

	public static void main(String[] args) {
		
		double a=15.3;
		double b=3;
		
		DecimalFormat df=new DecimalFormat("0.00");
		String s= df.format(a/b);
		//double reg=Double.valueOf(s);
		System.out.println(s);

	}

}

你可能感兴趣的:(Java学习笔记)