练习题do-while(购物商品)
2019-03-19 本文已影响0人
MAG沐汐
购物商品例题:
package ad;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Ts1 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //实例化Scanner
DecimalFormat df = new DecimalFormat("0.0"); //保留1位小数
int amount = 0; //数量
double discount =0.8; //折
double total =0.0; //总价
double payment =0.0; //实付
//价格
double shirt = 188.0;
double tennis = 280.0;
double racket = 150.0;
String k = null ;
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("*************");
System.out.print("请选择购买的商品编号:");
int id = input.nextInt();//编号
System.out.print("请选择购买的商品数量:");
amount = input.nextInt(); //数量
do {
//1.T血
if (id==1) {
System.out.println("T恤¥:"+shirt+" "+"数量:"+amount+" "+"合计¥:"+shirt*amount);
System.out.print("是否继续(y/n):");
k = input.next();
}
//2.网球鞋
if (id==2) {
System.out.println("网球鞋¥:"+tennis+" "+"数量:"+amount+" "+"合计¥:"+tennis*amount);
System.out.print("是否继续(y/n):");
k = input.next();
}
//3.网球拍
if (id==3) {
System.out.println("网球拍¥:"+racket+" "+"数量:"+amount+" "+"合计¥:"+racket*amount);
System.out.print("是否继续(y/n):");
k = input.next();
}
} //输入y继续执行循环,输入n结束循环,执行对应的编号商品进行结账
while (k=="y");
//结账T血
if (id==1) {
total = (shirt*amount)*discount;
payment = shirt*amount;
System.out.println("折扣:"+discount);
System.out.println("应付金额:"+df.format(total));
System.out.println("实付金额:"+payment);
System.out.println("找钱:"+df.format(payment-total));
}
//结账网球鞋
if (id==2) {
total = (tennis*amount)*discount;
payment = tennis*amount;
System.out.println("折扣:"+discount);
System.out.println("应付金额:"+df.format(total));
System.out.println("实付金额:"+payment);
System.out.println("找钱:"+df.format(payment-total));
}
//结账网球拍
if (id==3) {
total = (racket*amount)*discount;
payment = racket*amount;
System.out.println("折扣:"+discount);
System.out.println("应付金额:"+df.format(total));
System.out.println("实付金额:"+payment);
System.out.println("找钱:"+df.format(payment-total));
}
}
}
运行结果:
do-while
java for循环学习:for循环