java新手求大神解答。主要求代码,有思路但是写不出啊。
发布网友
发布时间:2023-07-18 00:22
我来回答
共3个回答
热心网友
时间:2024-11-29 04:27
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
enum Huo {
TX(1, "T恤", 245), WQX(2, "网球鞋", 570), WQP(3, "网球拍", 320);
private int id;
private String name;
private double price;
private Huo(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public static Huo getById(int id) {
for (Huo item : Huo.values()) {
if (item.id == id) {
return item;
}
}
return null;
}
}
public class Test {
private static Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
private static double sum = 0;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("**********************************************");
System.out.println("请选择购买的商品编号:");
for (Huo item : Huo.values()) {
System.out.print(item.getId() + ":" + item.getName() + " ");
}
System.out.println();
System.out.println("**********************************************");
System.out.print("输入购买商品编号:");
int id = in.nextInt();
System.out.print("输入购买商品数量:");
int count = in.nextInt();
Integer tmp = countMap.get(id);
if (tmp == null) {
tmp = 0;
}
countMap.put(id, tmp + count);
show();
while (true) {
System.out.print("是否继续(y/n):");
String jixu = in.next();
if ("Y".equalsIgnoreCase(jixu)) {
break;
} else if ("N".equalsIgnoreCase(jixu)) {
System.out.print("请支付:");
double fu = in.nextDouble();
System.out.println("应付金额:" + sum);
System.out.println("客户支付:" + fu);
System.out.println("找钱:" + (fu - sum));
System.exit(0);
} else {
System.out.print("输入错误");
}
}
}
}
private static void show() {
Iterator<Integer> it = countMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
Integer count = countMap.get(key);
Huo huo = Huo.getById(key);
double heji = huo.getPrice() * count;
System.out.println(huo.getName() + "¥" + huo.getPrice() + " 数量:" + count + " 合计:" + heji);
sum += heji;
}
}
}
热心网友
时间:2024-11-29 04:27
import java.util.*;
class Shop
{
public static void main(String[] args)
{
int a,b,e,num=0;
String c="y",d="y",Y="y";
Scanner in=new Scanner(System.in);
System.out.print("请选择购买的商品编号:1.T恤 2.网球鞋 3.网球拍!\n**********************************************\n");
while (d.equals(Y))
{
while(c.equals(Y)){
System.out.print("请输入购买商品编号:");
a=in.nextInt();
System.out.print("请输入购买商品数量:");
b=in.nextInt();
if(a==1){
num+=245*b;
}else if(a==2){
num+=570*b;}
else{
num+=100*b;
}
System.out.print("是否继续(y/n):");
c=in.next();
}
System.out.println("合计¥:"+num);
System.out.print("是否继续(y/n):");
d=in.next();
}
System.out.println("应付金额:"+num);
System.out.println("顾客支付:");
e=in.nextInt();
System.out.println("找钱"+(e-num));
}
}
热心网友
时间:2024-11-29 04:28
我先问下,是否继续这个分支是用来循环输入购买商品跟数量的对吧?
那么比较简单的方法就是用一个变量来保存,在代码开头加入一句 float count=0;
去保存你的数值
需要在是否继续(y/n):y这句话前加入一个
count+=每次小结合计的数值
最后System.out.println("应付金额:"+count);