在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 【JAVA小白】 用eclipse輸入格式不匹配的問題

【JAVA小白】 用eclipse輸入格式不匹配的問題

問題如下:

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];//保存送餐時(shí)間
        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];//保存訂單的總金額
        //boolean end = false; 本來是拿來結(jié)束整個(gè)do-while循環(huán)的,現(xiàn)在作廢
        String button;
        Scanner input = new Scanner(System.in); 
        int choose;
        
        do {
            int check = 0;//檢查
            int check_ok = 0;//檢查到餐袋內(nèi)訂單的數(shù)量

            //程序初始化,打印菜單體系
            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、我要點(diǎn)贊");
            System.out.println("6、退出系統(tǒng)");
            System.out.println("********************************************");
            System.out.print("\n請(qǐng)選擇:");
            choose = input.nextInt(); //輸入菜單選項(xiàng)
            switch (choose) {
            case 1:
                System.out.println("***我要訂餐***");
                //先檢查一下餐袋時(shí)候已經(jīng)滿了,如果已經(jīng)滿了,則無法點(diǎn)餐
                
                for(;check < name.length;check++){//循環(huán)檢查餐袋,看看有沒有訂單
                    if (name[check] !=null){//如果不是空
                        check_ok += 1;//訂單數(shù)量加1
                    }else{//如果檢查到訂單數(shù)量為空,則進(jìn)入點(diǎn)餐
                        break;
                    }
                }
                if (check_ok == 4) {//餐袋數(shù)量已滿,終止點(diǎn)單
                    System.out.println("餐袋已滿,無法點(diǎn)單。");
                    //返回菜單界面
                }else{//餐單數(shù)量未滿,進(jìn)入點(diǎn)單
                    System.out.print("請(qǐng)輸入訂餐人姓名:");
                    name[check_ok] = input.next();//把名字放入姓名數(shù)組
    
                    System.out.println("序號(hào)\t菜名\t單價(jià)\t點(diǎn)贊數(shù)");
                    System.out.println("1\t紅燒帶魚\t38.0\t0");
                    System.out.println("2\t魚香肉絲\t20.0\t0");
                    System.out.println("3\t時(shí)令鮮蔬\(yùn)t10.0\t0");
    
                }
                System.out.print("請(qǐng)選擇您要點(diǎn)的菜品編號(hào):");
                int number = input.nextInt();
                System.out.print("請(qǐng)輸入您需要的份數(shù):");
                int copies = input.nextInt();
                switch (number) {
                case 1:
                    dishMegs[check_ok] = "紅燒帶魚" +copies + "份";
                    sumPrices1[check_ok] = 38 * copies;
                    break;
                case 2:
                    dishMegs[check_ok] = "魚香肉絲" +copies + "份";
                    sumPrices1[check_ok] = 20 * copies;
                    break;
                case 3:
                    dishMegs[check_ok] = "時(shí)令鮮蔬" +copies + "份";
                    sumPrices1[check_ok] = 10 * copies;
                    break;
                default:
                    System.out.println("請(qǐng)輸入1-3的數(shù)字");
                    break;
                }
                if (sumPrices1[check_ok] < 50) {
                    sumPrices[check_ok] = sumPrices1[check_ok] + SEND_PRICES;
                }
    
                System.out.println("請(qǐng)輸入送餐時(shí)間(送餐時(shí)間是10點(diǎn)至20點(diǎn)之間的整點(diǎn)):");
                
                boolean time_check_ok = true; //設(shè)置一個(gè)循環(huán)指令,如果輸入的時(shí)間合理,則停止循環(huán),否則就繼續(xù)輸入
                do {
                    int time_check = input.nextInt();    
                    if (time_check <= 20 && time_check>= 10) {
                        times[check_ok] = time_check;
                        time_check_ok = false;
                    }else {
                        System.out.println("請(qǐng)重新輸入正確的送餐時(shí)間:");
                    }    
                } while(time_check_ok);
                System.out.println("請(qǐng)輸入送餐地址:");
                addresses[check_ok] = input.next();
                
                System.out.println("訂餐成功!");
                System.out.println("您訂的是:" + dishMegs[check_ok]);
                System.out.println("送餐時(shí)間:" + times[check_ok]);
                System.out.println("送餐地址:" + addresses[check_ok]);
                System.out.println("餐費(fèi):" + sumPrices1[check_ok] + "元,送餐費(fèi):" + SEND_PRICES + ",總計(jì):" + sumPrices[check_ok] + "元。");
                
                break;
            case 2:
                System.out.println("***查看餐袋***");
                System.out.println("序號(hào)\t訂餐人\t餐品信息\t送餐時(shí)間\t送餐地址\t總金額\t訂單狀態(tài)");
                
                for(;check < name.length;check++){//循環(huán)檢查餐袋,看看有沒有訂單
                    if (name[check] !=null){//如果不是空
                        check_ok += 1;//訂單數(shù)量加1
                    }else{//如果檢查到訂單數(shù)量為空,則計(jì)數(shù)完畢
                        break;
                    }
                }
                for (int serialNumber = 0; serialNumber < check_ok; serialNumber++) {
                    System.out.println((serialNumber+1) + "\t" + name[serialNumber] + "\t" + dishMegs[serialNumber] + "\t" + 
                            times[serialNumber] + "\t" + addresses[serialNumber] + "\t" + sumPrices[serialNumber] + "\t" + states[serialNumber]);
                }
                break;
            case 3:
                System.out.println("簽收");
                break;
            case 4:
                System.out.println("刪除");
                break;
            case 5:
                System.out.println("點(diǎn)贊");
                break;
            case 6:
                System.out.println("退出");
                break;
            default:
                System.out.println("請(qǐng)輸入1-6當(dāng)中的數(shù)字");
                break;
            }
            //System.out.print("輸入y返回:");
            //button = input.next();//這里有問題  還沒有解決
        } while (true );
        //button.equals("y")
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
    
    
}

這個(gè)代碼內(nèi)容不是重點(diǎn)
重點(diǎn)是每次讓我輸入的時(shí)候
比如nextInt()
我輸入一個(gè)1
經(jīng)常給我報(bào)錯(cuò):

clipboard.png
網(wǎng)上查了一下,說是輸入的類型和接收的類型不匹配,所以導(dǎo)致錯(cuò)誤。
也就是說,很可能我輸入的1,是系統(tǒng)以為我輸入的是一個(gè)字符串的1 然后就崩掉了

老遇到這個(gè)問題,想問下各位大神到底是怎么解決這個(gè)問題的?

回答
編輯回答
澐染

復(fù)制過來是對(duì)的。不知題主是怎么觸發(fā)的。題主說“經(jīng)常給我報(bào)錯(cuò)”,看來并不是100%觸發(fā)??茨芊裾业揭粋€(gè)必定觸發(fā)的條件。

2018年3月5日 04:04