解决方案 »

  1.   

    package testString;import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    import java.util.Stack;public class Menu {
    private static final Menu Do = null;
    static Map<Integer, Food> is = new HashMap<Integer, Food>();
    static int[] array = new int[10];
    static int m = 0;
    static int sum = 0;
    Food p1 = new Food("大白菜", 10);
    Food p2 = new Food("大龙虾", 50);
    Food p3 = new Food("回锅肉", 30);
    Food p4 = new Food("麻婆豆腐", 22);
    Food p5 = new Food("番茄鸡蛋", 12);
    Food p6 = new Food("炒猪耳朵", 26);
    Food p7 = new Food("宫爆鸡丁", 35);
    Food p8 = new Food("干锅豆腐", 18);
    Food p9 = new Food("烤羊串", 3);
    Food p0 = new Food("萝卜汤", 15); public void show() {
    is.put(1, p1);
    is.put(2, p2);
    is.put(3, p3);
    is.put(4, p4);
    is.put(5, p5);
    is.put(6, p6);
    is.put(7, p7);
    is.put(8, p8);
    is.put(9, p9);
    is.put(0, p0);
    } public ArrayList<Food> shoo() {
    ArrayList<Food> food = new ArrayList<Food>();
    new Menu().show();
    Random ra = new Random();
    for (int i = 0; i < array.length; i++) {
    int aa = ra.nextInt(10);
    array[i] = aa;
    food.add(is.get(aa));
    sum += food.get(i).getPrice();
    System.out.println(food.get(i));
    }
    System.out.println("一共需要" + sum + "元");
    return food;
    } public void Do() {
    ArrayList<Food> foodShelf = new ArrayList<Food>();//??????????
    ArrayList<Food> food = new Menu().shoo();
    while (true) {
    if (food.size() != 0) {
    if (foodShelf.size() >= 5) {//这里为0
    while (true) {
    if (foodShelf.size() != 0) {
    System.out.println(foodShelf.get(0).toString(
    foodShelf.size() - 1, food.size()));
    foodShelf.remove(0);
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    break;
    }
    }
    }
                  System.out.println("出错了");//=======================看这里============================
    foodShelf.add(food.get(0));//你在这里不停的取.取完数参数,却没做任何处理!取不到参数所以报空针异常.
    System.out.println(foodShelf.get(m).toString(foodShelf.size()));
    food.remove(0);
    m++;
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    System.exit(0);
    }
    }
    }
    }
    看上面的注释!