老师,我写好代码后点击运行,一点反应都没有,我实在查不出来哪有问题,老师帮我看看
//商品类 package com.shopping.management; //商品类 public class Goods { //属性 private String goodsId; private String goodsName; private double price; private String goodsDesp; //构造方法 public Goods(String goodsId, String goodsName, double price, String goodsDesp) { super(); this.goodsId = goodsId; this.goodsName = goodsName; this.price = price; this.goodsDesp = goodsDesp; } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this.goodsDesp = goodsDesp; } //重写toString方法 @Override public String toString() { return "编号:" + goodsId + ", 名称:" + goodsName + ", 价格:" + price + ", 描述:" + goodsDesp + "]"; } @Override //重写hashCode方法 public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((goodsDesp == null) ? 0 : goodsDesp.hashCode()); result = prime * result + ((goodsId == null) ? 0 : goodsId.hashCode()); result = prime * result + ((goodsName == null) ? 0 : goodsName.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } //重写equals方法 @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj.getClass()==Goods.class) { Goods goods = (Goods)obj; return (goods.getGoodsDesp()).equals(goodsDesp)&&(goods.getGoodsId()).equals(goodsId)&&(goods.getGoodsName()).equals(goodsName)&&goods.getPrice()==price; } return false; } } //商品管理类 package com.shopping.management; //商品管理类 import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class GoodsManage { //存放商品的容器 private Set goodsSet; //实例化商品对象 Goods one=new Goods("goods004","手机",2300.0,"android手机"); Goods two=new Goods("goods002","饮水机",299.0,"带净化功能的饮水机"); Goods three=new Goods("goods003","笔记本电脑",4999.0,"15寸笔记本电脑"); Goods four=new Goods("goods001","水杯",56.0,"不锈钢水杯"); public GoodsManage() { } public GoodsManage(Set goodsSet) { super(); this.goodsSet = goodsSet; } public Set getGoodsSet() { return goodsSet; } public void setGoodsSet(Set goodsSet) { this.goodsSet = goodsSet; } //导入商品信息 public void importGoods() { goodsSet=new HashSet(); goodsSet.add(one); goodsSet.add(two); goodsSet.add(three); goodsSet.add(four); } //显示商品信息 public void displayAllGoods() { Iterator it=goodsSet.iterator(); while(it.hasNext()) { System.out.println("所有商品的信息为:"); System.out.println(it.next()); } } } //购物车中商品信息类 package com.shopping.management; //购物车中商品信息类 public class GoodsInCart { //属性 private Goods goods; private int num; //构造方法 public GoodsInCart(Goods goods, int num) { super(); this.goods = goods; this.num = num; } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } //购物车类 package com.shopping.management; //购物车类 import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class ShoppingCart { //购物车属性,String类型的key,和GoodsInCart类型的value private Map <String,GoodsInCart>shoppingCart=new HashMap<String,GoodsInCart>(); //构造方法 public ShoppingCart() { } public ShoppingCart(Map<String, GoodsInCart> shoppingCart) { super(); this.shoppingCart = shoppingCart; } public Map<String, GoodsInCart> getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) { this.shoppingCart = shoppingCart; } //添加商品到购物车方法 public void addGoodsToCart(GoodsManage gm) { Set <Goods> set=gm.getGoodsSet(); Scanner sc=new Scanner(System.in); String str=null; Goods goods=null; boolean flag=false; if(set.size()==0) { System.out.println("请先导入商品信息!"); } else { gm.displayAllGoods(); System.out.println("请输入要添加的商品编号:"); str=sc.next(); for(Goods good:set) { if(good.getGoodsId().equals(str)) { flag=true; goods=good; break; } } if(flag=true) { System.out.println("请输入要添加商品的数量:"); int num=sc.nextInt(); GoodsInCart goodsInCart=new GoodsInCart(goods,num); shoppingCart.put(str, goodsInCart); } } } //修改购物车中的商品数量方法 public void updateNumInCart() { System.out.println("请输入要修改的商品编号:"); Scanner sc=new Scanner(System.in); Set<String>keySet=shoppingCart.keySet(); int n=0; for(String key:keySet) { if(key.equals(sc.next())) { System.out.println("请输入要修改的商品数量:"); n=sc.nextInt(); if(n==0) { keySet.remove(key); System.out.println("因为该商品数量为零,所以该商品已移除!"); } else { shoppingCart.get(key).setNum(n); } } } } //显示购物车中的所有商品方法 public void displayAllInCart() { Set<Entry<String,GoodsInCart>> entrySet=shoppingCart.entrySet(); if(entrySet.size()==0) { System.out.println("您还未添加任何商品,快去挑选吧!"); } else { for(Entry<String,GoodsInCart>entry:entrySet) { System.out.println(entry.getKey()+entry.getValue()+"个"); } } } //结算方法 public void settleAccounts() { double allPrice=0; Set<Entry<String,GoodsInCart>> entrySet=shoppingCart.entrySet(); for(Entry<String,GoodsInCart>entry:entrySet) { allPrice+=((entry.getValue().getNum())*(entry.getValue().getGoods().getPrice())); } System.out.println("商品的总价为:"+allPrice); for(Entry<String,GoodsInCart>entry:entrySet) { System.out.println(entry.getKey()+entry.getValue()+"个"); } } //属性: // //- 购物车(shoppingCart):Map类型,其中key为String类型,value为GoodsInCart类型 // //方法: // //-添加商品到购物车: // // public void addGoodsToCart(GoodsManage gm); // //-修改购物车中的商品数量:public void updateNumInCart(); // // - 显示购物车中的所有商品:public void displayAllInCart(); // // - 结算:public void settleAccounts(); // // - 构造方法及getter和setter方法 } //测试类 package com.shopping.management; import java.util.InputMismatchException; import java.util.Scanner; public class TestDemo { public void menu() { System.out.println("**********************************"); System.out.println("***主菜单***"); System.out.println("1--商品管理"); System.out.println("2--购物车"); System.out.println("0--退出"); System.out.println("**********************************"); } public void menu2() { System.out.println("**********************************"); System.out.println("***商品管理***"); System.out.println("1--商品信息导入"); System.out.println("2--显示所有商品信息"); System.out.println("9--返回上一级菜单"); System.out.println("**********************************"); } public void menu3() { System.out.println("**********************************"); System.out.println("***购物车管理***"); System.out.println("1--添加商品到购物车"); System.out.println("2--修改购物车中的商品数量"); System.out.println("3--显示购物车中的所有商品信息"); System.out.println("4--结算"); System.out.println("9--返回上一级菜单"); System.out.println("**********************************"); } public static void main(String[] args) { // TODO Auto-generated method stub boolean flag=false; TestDemo a=new TestDemo(); GoodsManage b=new GoodsManage(); ShoppingCart c=new ShoppingCart(); Scanner sc=new Scanner(System.in); int n=0; while (flag=false) { a.menu(); System.out.println("请输入相应的数字进行操作"); n=sc.nextInt(); flag=true; try { if(n<0||n>2) { System.out.println("您的输入有误,请重新输入!"); flag=false; continue; }else { switch(n) { case 1: while(flag=true) { a.menu2(); System.out.println("请输入相应的数字进行操作"); if(n<1||n>2&&n!=9) { System.out.println("您的输入有误,请重新输入!"); continue; }else { switch(n) { case 1: b.importGoods(); break; case 2: b.displayAllGoods(); break; } } if(n==9) { flag=false; break; } } break; case 2: while(flag=true) { a.menu3(); System.out.println("请输入相应的数字进行操作"); n=sc.nextInt(); if(n<1||n>4&&n!=9) { System.out.println("您的输入有误,请重新输入!"); continue; }else { switch(n) { case 1: c.addGoodsToCart(b); break; case 2: c.updateNumInCart(); break; case 3: c.displayAllInCart(); break; case 4: c.settleAccounts(); break; } } if(n==9) { flag=false; break; } } break; case 0: System.out.println("***欢迎您下次光临***"); break; } } }catch(InputMismatchException an){ System.out.println("你的输入有误,请重新输入!"); } } } }
0
收起
正在回答
2回答
同学你好,在同学的测试类的主方法中,while()循环时,while条件是flag = false是赋值语句,不是条件,所以不会执行循环,出现同学所说的没有反应,这里应该修改为==,如:
同学修改一下再试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
好帮手慕阿满
2019-03-05 16:58:19
public class Demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); while(true) { try { int n = sc.nextInt(); }catch (Exception e) { System.out.println("您的输入有误,请重新输入!"); sc.next();//接收错误的数据 continue; } } } }
同学可以参考如上代码,定义一个循环,使用try-catch,当输入错误的信息时,执行catch语句块,先输入错误提示,然后使用sc.next()将错误数据接收,调用continue继续循环。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星