while循环
作者:互联网
import java.util.*;
public class guowu{
public static void main(String []args){
System.out.println("********************************************");
System.out.println("请选择购买商品的编号:");
System.out.println("1.T恤\t\t2.网球鞋\t3.网球拍");
System.out.println("********************************************");
Scanner sc = new Scanner(System.in); //创建对象
String jixv ="y"; //定义是否继续购买
int spb =0; //定义储存商品的编号的变量
int sps =0; //定义储存商品的数量的变量
double tixu = 245.0; //定义T恤价格
double wangx = 570.0; //定义网球鞋价格
double wangq = 320.0; //定义网球拍价格
double spd =0; //定义每次购买商品的总价
double spz =0; //定义购买商品的总价
while("y".equals(jixv)){
System.out.println(); //为了运算时画面精美
System.out.print("请输入商品的编号:");
spb =sc.nextInt(); //接受数据
System.out.print("请输入购买数量:");
sps =sc.nextInt(); //接受数据
switch(spb){
case 1:
spd = tixu * sps;
System.out.println("T恤"+"¥"+tixu+"\t"+"数量:"+sps+"\t"+"合计"+"¥"+spd);
spz += spd;
spd = 0; //数据清0
break;
case 2:
spd = wangx * sps;
System.out.println("网球鞋"+"¥"+wangx+"\t"+"数量:"+sps+"\t"+"合计"+"¥"+spd);
spz += spd;
spd = 0;
break;
case 3:
spd = wangq * sps;
System.out.println("网球拍"+"¥"+wangq+"\t"+"数量:"+sps+"\t"+"合计"+"¥"+spd);
spz += spd;
spd = 0;
break;
}
System.out.print("是否继续(y/n):");
jixv = sc.next();
}
System.out.println(); //为了运算时画面精美
System.out.print("折扣:");
double zk = sc.nextDouble();
spz *= zk;
System.out.println("应付金额:"+spz);
System.out.print("实付金额:");
double sf = sc.nextDouble();
if(sf < spz){
System.out.print("您输入的金额小于应付金额,请重新输入:");
sf = sc.nextDouble();
}
System.out.println("找钱:"+(sf-spz));
}
}
标签:spz,sps,System,while,循环,println,spd,out 来源: https://blog.csdn.net/Ylimsh/article/details/121512941