鍍金池/ 問答/Java  HTML/ 【JAVA小白】 做書上的項目,一個訂餐系統(tǒng),遇到bug

【JAVA小白】 做書上的項目,一個訂餐系統(tǒng),遇到bug

代碼如下,還沒寫完:

import java.util.Scanner;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

public class Restaurant_system {
    public static void main(String[] args) {
        /**吃貨聯(lián)盟訂餐系統(tǒng)
        */
        /*數(shù)據(jù)存放體系,以及需要用到的功能 */
        String[]name = new String[4]; //保存訂餐人姓名
        String[]dishMegs = new String[4];//保存所選信息,包括菜品名以及份數(shù)
        int[]times = new int[4];//保存送餐時間
        String[]addresses = new String[4];//保存送餐地址
        int[]states = new int[4];//保存訂單狀態(tài):0表示已經(jīng)預(yù)定,1表示已經(jīng)完成
        double[]sumPrices1 = new double[4];//保存訂單菜品金額
        final double SEND_PRICES = 5.;
        double[]sumPrices = new double[4];//保存訂單的總金額
        Scanner input = new Scanner(System.in); 
        int choose;
        
        //程序初始化,打印菜單體系
        System.out.println("歡迎使用“吃貨聯(lián)盟訂餐系統(tǒng)”");
        System.out.println("********************************************");
        System.out.println("1、我要訂餐");
        System.out.println("2、查看餐袋");
        System.out.println("3、簽收訂單");
        System.out.println("4、刪除訂單");
        System.out.println("5、我要點贊");
        System.out.println("6、退出系統(tǒng)");
        System.out.println("********************************************");
        System.out.print("\n請選擇:");
        choose = input.nextInt(); //輸入菜單選項
        switch (choose) {
        case 1:
            System.out.println("***我要訂餐***");
            //先檢查一下餐袋時候已經(jīng)滿了,如果已經(jīng)滿了,則無法點餐
            int check = 0;//檢查
            int check_ok = 0;//檢查到餐袋內(nèi)訂單的數(shù)量
            for(;check < name.length;check++){//循環(huán)檢查餐袋,看看有沒有訂單
                if (name[check] !=null){//如果不是空
                    check_ok += 1;//訂單數(shù)量加1
                }else{//如果檢查到訂單數(shù)量為空,則進入點餐
                    break;
                }
            }
            if (check_ok == 4) {//餐袋數(shù)量已滿,終止點單
                System.out.println("餐袋已滿,無法點單。");
                //返回菜單界面
            }else{//餐單數(shù)量未滿,進入點單
                System.out.print("請輸入訂餐人姓名:");
                name[check] = input.next();//把名字放入姓名數(shù)組

                System.out.println("序號\t菜名\t單價\t點贊數(shù)");
                System.out.println("1\t紅燒帶魚\t38.0\t0");
                System.out.println("2\t魚香肉絲\t20.0\t0");
                System.out.println("3\t時令鮮蔬\t10.0\t0");

            }
            System.out.print("請選擇您要點的菜品編號:");
            int number = input.nextInt();
            System.out.print("請輸入您需要的份數(shù):");
            int copies = input.nextInt();
            switch (number) {
            case 1:
                dishMegs[check] = "紅燒帶魚" +copies + "份";
                sumPrices1[check] = 38 * copies;
                break;
            case 2:
                dishMegs[check] = "魚香肉絲" +copies + "份";
                sumPrices1[check] = 20 * copies;
                break;
            case 3:
                dishMegs[check] = "時令鮮蔬" +copies + "份";
                sumPrices1[check] = 10 * copies;
                break;
            default:
                System.out.println("請輸入1-3的數(shù)字");
                break;
            }
            if (sumPrices1[check] < 50) {
                sumPrices[check] = sumPrices1[check] + SEND_PRICES;
            }

            System.out.println("請輸入送餐時間(送餐時間是10點至20點之間的整點):)");
            
            boolean time_check_ok = true; //設(shè)置一個循環(huán)指令,如果輸入的時間合理,則停止循環(huán),否則就繼續(xù)輸入
            do {
                int time_check = input.nextInt();    
                if (time_check <= 20 || time_check>= 10) {
                    times[check] = time_check;
                    time_check_ok = false;
                }else {
                    System.out.println("請重新輸入正確的送餐時間:");
                }    
            } while(time_check_ok);
            System.out.print("請輸入送餐地址:");
            addresses[check] = input.next();
            
            System.out.println("訂餐成功!");
            System.out.print("您訂的是:" + dishMegs[check]);
            System.out.println("送餐時間:" + times[check]);
            System.out.println("餐費:" + sumPrices1 + "元,送餐費:" + SEND_PRICES + ",總計:" + sumPrices + "元。");
            System.out.println("輸入0返回:");
            

            //下面還沒寫完,目前測試的時候,上面部分出現(xiàn)問題。
            break;
        case 2:
            System.out.println("餐袋");
            break;
        case 3:
            System.out.println("簽收");
            break;
        case 4:
            System.out.println("刪除");
            break;
        case 5:
            System.out.println("點贊");
            break;
        case 6:
            System.out.println("退出");
            break;
        default:
            System.out.println("請輸入1-6當(dāng)中的數(shù)字");
            break;
        }
    }
    

執(zhí)行程序:

clipboard.png
要求輸入選項,輸入1
然后彈出,要求輸入名字:
輸入一個名字,比如小明

clipboard.png

彈出如下錯誤,檢查了一邊代碼,沒發(fā)現(xiàn)什么問題,具體錯在哪兒呢?

回答
編輯回答
憶往昔

我知道問題所在了,是eclipse本身的bug,輸入的時候要把光標(biāo)移動到冒號后面

2017年5月15日 12:54
編輯回答
挽歌

控制臺說了在32行代碼報錯, choose = input.nextInt();這行代碼
Scanner里面的報錯代碼,nextInt方法開始

 public String next(Pattern pattern) {
        ensureOpen();
        if (pattern == null)
            throw new NullPointerException();

        // Did we already find this pattern?
        if (hasNextPattern == pattern)
            return getCachedResult();
        clearCaches();

        // Search for the pattern
        while (true) {
            String token = getCompleteTokenInBuffer(pattern);
            if (token != null) {
                matchValid = true;
                skipped = false;
                return token;
            }
            if (needInput)
                readInput();
            else
                throwFor();//最后在這行代碼拋出異常。
        }
    }

InputMismatchException異常,如果下一個標(biāo)記與 Integer 正則表達式不匹配,或者超出范圍,會拋出此異常。第一次要輸入的是1-6的整數(shù)。

2017年12月22日 16:11