请问哪里出错了,该怎么修改,以及建议的地方。。。不胜感激
import java.util.Scanner; class Store{ private String name; private double size; private double price; private String pz; private int have; public Store(){ } public void setName(String name) { this.name = name; } public void setSize(double size) { this.size = size; } public void setPrice(double price) { this.price = price; } public void setPz(String pz) { this.pz = pz; } public void setHave(int have) { this.have = have; } public String getName() { return name; } public double getSize() { return size; } public double getPrice() { return price; } public String getPz() { return pz; } public int getHave() { return have; } } public class test { public static void main(String[] args){ Store[] store; store = creatStore(); printStore(store); } public static Store[] creatStore(){ Store[] store = new Store[3]; Scanner input = new Scanner(System.in); for(int i=0; i<3; i++) { String name; double size; double price; String pz; int have; name = input.nextLine(); size = input.nextDouble(); price = input.nextInt(); pz = input.nextLine(); have = input.nextInt(); store[i].setName(name); store[i].setSize(size); store[i].setPrice(price); store[i].setPz(pz); store[i].setHave(have); } return store; } public static void printStore(Store[] store){ System.out.println("品牌型号\t 尺寸\t 价格\t 配置\t 库存"); for(int i=0; i<3; i++) { System.out.println(store[i].getName()+"\t"+store[i].getSize()+"\t"+store[i].getPrice()+"\t"+store[i].getPz()+"\t"+store[i].getHave()); } } }
![]() | 1 EscYezi 2019-09-11 00:46:03 +08:00 via iPhone 不要在 for 循环里面声明引用,放在 for 外面比较好。 还有出现的问题是什么?数据不对?编译错误?运行时错误?错误提示是什么? |
![]() | 2 zjp 2019-09-11 01:14:27 +08:00 via Android ![]() 没有期望输出也没有实际运行结果,你是希望别人人肉编译或者打开 IDE 执行一遍,再推测? 看一遍《提问的智慧》比刷几道题有用多了 |
3 Hattle 2019-09-11 01:14:54 +08:00 via Android for 循环里的 double 类型的 price 你用的是 nextInt,如果输入带小数点的数字肯定是会报错的吧。最后输出的时候可以改 foreach,那样写起来和看起来都更舒服点。 |
![]() | 4 ixx 2019-09-11 10:10:16 +08:00 是报空指针了吧 store[i] 你就这么直接用了? 啥时候初始化的? |
![]() | 6 tuboshuv1 2019-09-11 10:49:10 +08:00 没别的意思,这种自己看就能看出来的问题就别问了。 老出现这种问题,等你水平高了,你想问的问题就没人解答了。 |
7 ErrorMan 2019-09-11 13:20:02 +08:00 via Android @guyeu 教初学者的话还是直接让对方改掉这个习惯好,jvm 这么写也许 OK,但是这个写法本身还是不好的,换个语言可能就得炸 |
8 luoyezhishui 2019-09-11 14:06:05 +08:00 你至少要告诉我们,你这个是想要干什么吧。 输入是什么,期望输出是什么,结果是什么。 你就丢代码上来,大家也很为难啊。 |
![]() | 9 leafin 2019-09-11 15:03:09 +08:00 可能是课堂作业吧,懒的看 |
![]() | 11 xaplux 2019-09-12 04:21:37 +08:00 这代码让你写的稀碎。。。 |