
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
import javatextDecimalFormat;
import orgapachecommonslangStringUtils;
public class Test
{
public static void main(String []args){
Systemoutprintln("");
Systemoutprintln("请选择要购买的商品编号:");
Systemoutprintln("1T恤(245) 2网球鞋(570) 3网球拍(320)");
Systemoutprintln("");
Long total = 0L;
//应付费用和找零保留两位小数点
DecimalFormat df = new DecimalFormat("#00");
BufferedReader strin=new BufferedReader(new InputStreamReader(Systemin));
//接受用户输入的变量,商品编码、商品数量、是否继续和实际交费
String code = "";String num = "";String isBuy = "";String pay = "";
Double discount = 08;
int number = 0;Long payMoney = 0L;Boolean flag = false;
try
{
//dowhile循环,先执行do,然后在用户输入y的情况下循环执行do中的语句
do{
code ="";num="";isBuy="";
//如果输入编号有误则循环重新输入
do{
flag=false;
Systemoutprint("请输入商品编号:"+code);
//获取用户输入的编码
code = strinreadLine();
if(codeequals("1")||codeequals("2")||codeequals("3")){
flag = true;
}else{
flag=false;
Systemoutprint("商品编号不存在!");
}
}while(flag == false);
//输入不符合要求时循环重新输入
do{
flag = false;
Systemoutprint("请输入商品数量:"+num);
//获取用户输入的数量
num = strinreadLine();
//在输入数量不为空的情况下,将数量转换成整型,若转换失败说明输入的不是整型数据,提示用户重新输入
if(StringUtilsisNotEmpty(num)){
try
{
number = IntegerparseInt(num);
flag = true;
}
catch (Exception e)
{
Systemoutprintln("商品数量请输入正整数!");
flag = false;
num = "";
}
}
}while(flag == false);
//根据用户输入情况计算当前费用
if(codeequals("1")){
total += 245number;
Systemoutprintln("T恤 ¥"+2450 +" ¥"+total);
Systemoutprint("是否继续(y/n)"+isBuy);
isBuy = strinreadLine();
}else if(codeequals("2")){
total+=570number;
Systemoutprintln("网球鞋 ¥"+5700 +" ¥"+total);
Systemoutprint("是否继续(y/n)"+isBuy);
isBuy = strinreadLine();
}else if(codeequals("3")){
total+=361number;
Systemoutprintln("网球拍 ¥"+3200 +" ¥"+total);
Systemoutprint("是否继续(y/n)"+isBuy);
isBuy = strinreadLine();
}
}while(isBuyequals("y"));
Systemoutprintln("折扣:"+discount);
//根据折扣计算顾客需要交纳的费用
String needPay = dfformat(totaldiscount);
Systemoutprintln("金额总计: ¥"+needPay);
//输入不符合要求时循环重新输入
do{
flag =false;
Systemoutprint("实际交费: ¥"+pay);
//获取用户输入的实际交费
pay = strinreadLine();
//在输入交费不为空的情况下,将交费转换成长整型,若转换失败说明输入的不是长整型数据,提示用户重新输入
if(StringUtilsisNotEmpty(pay)){
try{
payMoney = LongparseLong(pay);
flag=true;
}catch(Exception ex){
Systemoutprintln("实际交费请输入数字!");
flag=false;
pay="";
}
}
}while(flag == false);
Systemoutprint("找钱: ¥"+(payMoney-DoubleparseDouble(needPay)));
}catch (IOException e1){
e1printStackTrace();
}
}
}
以上是能达到输出要求的代码,有通过测试,比较初级的,希望能帮到楼主。
你是想要看到每次的消费,然后在看到总价吗那么你应该添加一个计数器,计算循环次数,把每次的zj添加到ArrayList中实现如下
import javautil;public class Shangjilianxi3 {
public static void main(String[] args) {
Scanner input = new Scanner(Systemin);
String name = "";
double danjia = 00;
int bianhao = 0;
int shuliang = 0;
double zhekou = 08;
double je = 00;
Systemoutprintln("");
Systemoutprintln("\n请选择购买的商品编号");
Systemoutprintln("");
String wwe = "y";
//把每次的zj添加到list中
List<Double> list = new ArrayList<Double>();
double zj = 0;
int index = 0;
while ("y"equals(wwe)) {
Systemoutprintln("\n请输入商品编号");
bianhao = inputnextInt();
Systemoutprintln("\n请输入购买数量");
shuliang = inputnextInt();
switch (bianhao) {
case 1:
name = "T桖";
danjia = 2450;
break;
case 2:
name = "网球鞋";
danjia = 5700;
break;
case 3:
name = "网球拍";
danjia = 3200;
break;
}
zj = (danjia shuliang);
Systemoutprintln(name + "¥" + danjia + "\t数量" + shuliang + "合计¥"
+ zj);
//把每次zj添加到list中
listadd(zj);
Systemoutprintln("是否继续(y/n)");
wwe = inputnext();
//计算循环次数
index++;
}
//当大于一次时,把list中每个元素相加为zj
if (index >= 1) {
//把上面的zj清零
zj = 0;
for (int i = 0; i < listsize(); i++) {
zj += listget(i);
}
}
Systemoutprintln("折扣:" + zhekou);
Systemoutprintln("应付金额" + zj);
zj = zj zhekou;
Systemoutprintln("实付金额" + zj);
je = inputnextInt();
Systemoutprintln("找钱" + (je - zj));
}
}
public static void splitMoney(Double unSplitMoney) {
if (unSplitMoney <= 0) {
Systemoutprintln("请输入正确的金额");
}
int unSplitMoneyCents = (int) (unSplitMoney 100);
int[] splitArray = {100 100, 50 100, 20 100, 10 100, 5 100, 2 100, 1 100, 5 10, 2 10, 1 10, 5, 2, 1};
String[] splitArrayName = {"100元", "50元", "20元", "10元", "5元", "2元", "1元", "5角", "2角", "1角", "5分", "2分", "1分"};
Systemoutprintln("您的输入金额为:" + unSplitMoney + "元,可以被兑换为:");
for (int i = 0; i < splitArraylength && unSplitMoneyCents > 0; i++) {
if (unSplitMoneyCents >= splitArray[i]) {
Systemoutprint(unSplitMoneyCents / splitArray[i] + "个" + splitArrayName[i]+",");
unSplitMoneyCents = unSplitMoneyCents % splitArray[i];
}
}
}
测试用例:
public static void main(String[] args) {
splitMoney(12832);
}
输出结果:
您的输入金额为:12832元,可以被兑换为:
1个100元,1个20元,1个5元,1个2元,1个1元,1个2角,1个1角,1个2分,
Process finished with exit code 0
public static final Object[][] MAP = { { 20f, "两元" }, { 10f, "一元" },
{ 05f, "五角" }, { 02f, "两角" }, { 01f, "一角" }, { 005f, "五分" },
{ 002f, "两分" }, { 001f, "一分" } };
public static final float TOTAL = 50f;
这个能解决问题吗?
public static String getChange(float price) {
float temp = TOTAL - price;
StringBuffer sb = new StringBuffer();
for (Object[] entry : MAP) {
int i = 0;
float key = (Float) entry[0];
while ((temp - key) > 0) {
temp -= key;
i++;
}
if (i > 0)
sbappend(entry[1])append(i)append("张,");
}
return sbsubstring(0,sblength()-1);
}
package comlwthread;
/
银行账户类Account(不能透支),
包含账号id(10~16位数字),密码password(6位数字),户主姓名name,余额balence
/
public class Account {
private String id;
private int password;
private String name;
private double balence;
public String getId() {
return id;
}
public void setId(String id) {
thisid = id;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
thispassword = password;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public double getBalence() {
return balence;
}
public void setBalence(double balence) {
thisbalence = balence;
}
/
默认构造账户信息为:1111111111111111,666666,钱三多,88888888。
/
public Account() {
super();
thisid = "1111111111111111";
thispassword = 666666;
thisname = "钱三多";
thisbalence = 88888888;
}
/
另一个构造方法带4个参数分别初始化4个属性(带数据有效性验证)。
/
public Account(String id, int password, String name, double balence) {
thisid = id;
thispassword = password;
thisname = name;
thisbalence = balence;
}
/
查询余额
/
public static double selectMoney(Account account) {
return accountgetBalence();
}
/
存钱
/
public static String setMoney(Account account, double balence) {
if (balence < 0) {
return "存钱失败,请正确放入!";
}
double d = balence + accountgetBalence();
accountsetBalence(d);
return "您存入了" + balence + "元,现账户余额为+" + d;
}
/
取钱
/
public static String getMoney(Account account, double balence) {
double d = accountgetBalence();
if (balence > d) {
return "您的余额不足!";
}
accountsetBalence(d - balence);
return "您取出了" + balence + "元,现账户余额为+" + accountgetBalence();
}
}
以上就是关于怎么用JAVA编译升级购物结算 有商品名称、商品金额、继续购买的循环、折扣、总计金额、交费金额、找钱全部的内容,包括:怎么用JAVA编译升级购物结算 有商品名称、商品金额、继续购买的循环、折扣、总计金额、交费金额、找钱、java语句中循环问题,求多次循环的总价。、用java 完成整钱兑零钱程序,要求:输入一个double值,代表总零钱数,输出清单,列出等价的货等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)