JAVA中do-while循环结构简单应用
do-while简单使用
升级MyShopping.购物结算
package DEOM1;
import java.util.*;
public class tesk {
/**
* @param args
*/
public static void main(String[] args) {
//定义声明变量值
String scp1 = "机箱";
String scp2 = "电源";
String scp3 = "内存";
String scp4 = "主板";
String scp5 = "固态";
String scp6 = "显卡";
//对商品价格名称进行变量定义
//方便日后修改添加
int scp1NO = 100;
int scp2NO = 200;
int scp3NO = 300;
int scp4NO = 400;
int scp5NO = 500;
int scp6NO = 600;
double total = 0; //初始化商品总价
String anwser; //键盘获取选择Y或者N
double discount = 0.8; //定义商品折扣
Scanner input = new Scanner(System.in); //Scanner键盘获取选择编号
System.out.println("______________________________________");
System.out.println("\t MyShopping 管理系统 >> 购物结算 \t");
System.out.println("______________________________________");
//输出商品信息。。。。。。。。。。。。。。。。。。名称和价格。。。。。。。。。。。。。。。。。
System.out.println("\t____请输入编号选择购买商品____");
System.out.println("\t1./" + scp1 + scp1NO + "¥" + "\t\t" + "2./" + scp2 + scp2NO + "¥" + "\t");
System.out.println("\t3./" + scp3 + scp3NO + "¥" + "\t\t" + "4./" + scp4 + scp4NO + "¥" + "\t");
System.out.println("\t5./" + scp5 + scp5NO + "¥" + "\t\t" + "6./" + scp6 + scp6NO + "¥" + "\t");
System.out.println("______________________________________");
do { //do while 循环结构
System.out.println("\t请输入商品编号:////");
System.out.println("______________________________________");
int Numbering = input.nextInt();
if (Numbering == 1) { //选择商品1
System.out.println("请输入购买数量:");
int amount = input.nextInt();
double price = scp1NO * amount;
System.out.println("\t" + "数量:" + amount + " " + "合计:" + price);
total = total + price;
} else if (Numbering == 2) { //选择商品2
System.out.println("请输入购买数量:");
int amount = input.nextInt();
double price = scp2NO * amount;
System.out.println("\t" + "数量:" + amount + " " + "合计:" + price);
total = total + price;
} else if (Numbering == 3) { //选择商品3
System.out.println("请输入购买数量:");
int amount = input.nextInt();
double price = scp3NO * amount;
System.out.println("\t" + "数量:" + amount + " " + "合计:" + price);
total = total + price;
} else if (Numbering == 4) { //选择商品4
System.out.println("请输入购买数量:");
int amount = input.nextInt();
double price = scp4NO * amount;
System.out.println("\t" + "数量:" + amount + " " + "合计:" + price);
total = total + price;
} else if (Numbering == 5) { //选择商品5
System.out.println("请输入购买数量:");
int amount = input.nextInt();
double price = scp5NO * amount;
System.out.println("\t" + "数量:" + amount + " " + "合计:" + price);
total = total + price;
//嵌套if选择结构
} else if (Numbering == 6) { //选择商品6
System.out.println("\t 请输入购买数量:");
int amount = input.nextInt();
double price = scp6NO * amount;
System.out.println("\t" + "数量:" + amount + " " + "合计:" + price);
total = total + price;
} else {
System.out.println("________无此商品________"); //输入不存在商品编号时显示无此商品
}
System.out.println("\t 是否继续(Y/N)"); //输入Y继续输入N结束
anwser = input.next(); //键盘获取输入结果
} while (anwser.equals("Y"));
System.out.println("\t 请支付:" + total + "¥"); //输入Y进行结算
System.out.println("\t 请输入支付金额");
double Payment = input.nextDouble(); //键盘获取已支付金额
double money = total * discount; //计算应付金额 折扣 X 实付金额
System.out.println("\t 折扣:" + discount);
System.out.println("\t 应付金额:" + money);
System.out.println("\t 实付金额:" + total);
double Change = Payment - money; //找零计算 实付金额 - 应付金额
System.out.println("\t 找零:" + Change);
}
}