簡體   English   中英

我的switch語句中的一個案例不會結束

[英]A case in my switch statement won't end

我只需要創建一個方法,檢查三個整數是否相同,讓用戶告訴對象執行此方法,然后向用戶詢問另一個命令。 當我輸入“ c”時(僅在這種情況下執行此操作,在邏輯上相同的另一種情況下),它會執行預期的操作,但隨后將嘗試獲取下一個輸入並將其作為參數運行據我了解,已經執行的方法。

    something=in.nextLine();
    commands=something.charAt(0);
    do{
        switch(commands){
            //Blah blah blah other commands
            case 'c':
                boolean yes=object.allTheSame(in.nextInt(),in.nextInt(),in.nextInt());
                System.out.println(yes);
                System.out.println("Please enter a command: ");
                something=in.nextLine();
            break;
        }
        commands=something.charAt(0);
    }while(commands!='q'); 

您在那里的中斷將中斷switch語句,而不是do-while循環。

(由於您的比較似乎微不足道,因此我認為使用傳統的if-else更好。)

我不知道“其他命令”中的內容是什么,但是如果something的第一個字符不是c ,則commands顯然不會改變。 您可能要這樣做:

something=in.nextLine();
int pos = 0;
commands=something.charAt(pos++);
do{
    switch(commands){
        //Blah blah blah other commands
        case 'c':
            boolean yes=object.allTheSame(in.nextInt(),in.nextInt(),in.nextInt());
            System.out.println(yes);
            System.out.println("Please enter a command: ");
            something=in.nextLine();
        break;
    }
    commands=something.charAt(pos++);
}while(commands!='q'); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM