代码如下package 成员变量;
public class fyh {
int id;
String name;
double price;
int number;
}
package 成员变量;
import java.util.Scanner;
public class cybl {
fyh[] shopp = new fyh[20];
public static void main(String[] args) {
fyh[] shopp = new fyh[20]; // 定义一个购物车数组
Scanner a = new Scanner(System.in);
while (true) {
System.out.println("--------请输入要执行的功能-------");
System.out.println("添加:add");
System.out.println("查看:see");
System.out.println("修改:modify");
System.out.println("结算:pay");
String b = a.next();
switch (b) { // 用户所用到的功能
case "add":
add(shopp, a);
break;
case "see":
see(shopp);
break;
case "modify":
break;
case "pay":
break;
default:
System.out.println("没有此功能");
}
}
}
public static void add(fyh[] shopp, Scanner a) { // 增添商品的方法
fyh b = new fyh();
System.out.println("请你输入商品的编码");
b.id = a.nextInt();
System.out.println("请你输入商品名");
b.name = a.next();
System.out.println("请你输入商品的价格");
b.price = a.nextDouble();
System.out.println("请你输入购买商品的数量");
b.number = a.nextInt();
for (int i = 0; i < shopp.length; i++) // 将商品对象放到购物车数组里面去
{
if (shopp[i] != null) {
shopp[i] = b;
break; // 添加完毕,退出循环
}
}
System.out.println("你的商品添加完成");
}
public static void see(fyh[] shopp) // 列表展示
{
System.out.println("查询的列表如下");
System.out.println("商品编码\t\t\t商品名\t\t\t商品单价\t\t\t商品数量");
for (int i = 0; i < shopp.length; i++) {
fyh e = shopp[i];
if (e != null) {
System.out.println(e.id + "\t\t\t" + e.name + "\t\t\t" + e.price + "\t\t\t" + e.number);
// 查询商品
} else {
break;
}
}
}
public static void mocify(fyh[] shopp) // 修改购物车
{
}
}
把Switch里面see和add写完了,主要是测试add和see,但是怎么也运行不出来商品信息,求解