0

我只需要创建一个方法来检查三个整数是否相同,让用户告诉一个对象执行这个方法,然后向用户询问另一个命令。当我输入“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'); 
4

2 回答 2

1

你在那里的 break 会破坏 switch 语句而不是 do-while 循环。

(由于您的比较似乎微不足道,我认为使用传统的 if-else 是更好的形式。)

于 2013-11-01T18:30:23.473 回答
0

我不知道“Blah blah blah other commands”部分是什么,但如果somethingis not的第一个字符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'); 
于 2013-11-01T19:09:50.147 回答