微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从do内部的开关出来,但没有布尔值,这可能吗? Java的

所以代码是关于在do while中给出输入的限制.
在这种情况下,你有3个机会继续.在那之后do while停止,你也可以让oportunitie停止任何键加上输入,但是当你开始时,你是否有oportunitie(这里进入开关)退出程序.
问题或我遇到的问题就在这里.

也许这可能没有布尔,或者可能改变或添加我还不知道的东西.对不起,我试着找到一个答案,但我看到的只是关于出一个布局左右的循环.不是这样的.

Scanner kb = new Scanner(system.in);
    // c = continue
    char c;
    // attempt to limit the input to 3 times
    int attempt = 3;
    // option = op
    int op = 0;

    do {
        do{
            System.out.println("Choose continue[0] or go out[1].");
            while  (!kb.hasNextInt()) {
                kb.nextLine();
                System.out.println("It's not a number.");                   
            }
            op = kb.nextInt();
        } while ( op <= -1 || op >= 2 );

        switch (op) {
        case 0:
            System.out.println("Continue!");
            break;
        case 1:     //here I tried; attempt = -1  
            break;  //is where I think it needs to be something
        default:
            break;
        }

        System.out.println("Do you wanna try again,"+attempt+" less?[c]+enter\n"
                + "Any other key + enter to exit.");
        c = kb.next(".").toupperCase().charat(0);
        attempt--;
    } while ( attempt > 0 && ( c == 'C' ) );
            //here also to put !( op == 0 )
    kb.close();

解决方法

如果用户选择0,您只需要求继续.

Scanner kb = new Scanner(system.in);
// c = continue
char c = 'a';
// attempt to limit the input to 3 times
int attempt = 3;
// option = op
int op = 0;

do {
    do{
        System.out.println("Choose continue[0] or go out[1].");
        while  (!kb.hasNextInt()) {
            kb.nextLine();
            System.out.println("It's not a number.");
        }
        op = kb.nextInt();
    } while ( op <= -1 || op >= 2 );

    switch (op) {
        case 0:
            System.out.println("Continue!");
            System.out.println("Do you wanna try again,"+attempt+" less?[c]+enter\n"
                    + "Any other key + enter to exit.");
            c = kb.next(".").toupperCase().charat(0);
            attempt--;
            break;
        case 1:
            attempt = -1;
            break;
        default:
            break;
    }
} while ( attempt > 0 && ( c == 'C' ) );
kb.close();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐