老师,我写好代码后点击运行,一点反应都没有,我实在查不出来哪有问题,老师帮我看看

老师,我写好代码后点击运行,一点反应都没有,我实在查不出来哪有问题,老师帮我看看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//商品类
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("你的输入有误,请重新输入!");
        }
    }
 
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,在同学的测试类的主方法中,while()循环时,while条件是flag = false是赋值语句,不是条件,所以不会执行循环,出现同学所说的没有反应,这里应该修改为==,如:

http://img1.sycdn.imooc.com//climg/5c7e1c590001725c06540324.jpg

同学修改一下再试试。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 独孤求戒 提问者 #1
    如果在测试类中,我用try catch语句,在提示输入数字时候,我输入字母时候,系统会捕获异常从而结束程序运行,我想请问老师,在输入了字母的情况下,如何修改代码才能使程序重新循环,再次执行到System.out.println("您的输入有误,请重新输入!");语句,使程序不结束,继续运行
    2019-03-05 15:19:56
好帮手慕阿满 2019-03-05 16:58:19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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继续循环。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码