Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going out from a switch inside a do while, but without a boolean, it's possible? Java

So the code is about giving a limit of inputs in a do while. In this case you have 3 oportunities to continue. After that the do while stops, also you have the oportunitie to stop just presing any key plus enter, but also when you start, do you have the oportunitie (here enters the switch) to exit the program. The problem or where I get stuck is here.

That maybe it's possible without a boolean, or maybe changing or adding something that I don't yet know. Sorry I try to find an answer but all I saw it's about going out a while loop whith boolean or so. Not like this.

    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();
like image 463
mcGuffin Avatar asked Sep 09 '26 11:09

mcGuffin


2 Answers

I think you can do this pretty easily without a case switch by using a method:

    static Scanner kb = new Scanner(System.in);

public static void main(String args[]){
     if(getContinueOption(3)){
         //do what you want to do
     }
}

private static boolean getContinueOption(int attempts) {
    System.out.println("Would you like to continue? Y[1] : N[0]");
    while(attempts > 0){
        int input = kb.nextInt();
        if(input == 1){
            return true;
        }
        attempts--;
        System.out.println( (attempts == 0)? "Ok :(" : "Are you sure?");
    }
    return false;
}
like image 90
luckydog32 Avatar answered Sep 11 '26 01:09

luckydog32


You only need to ask for continuation if user chooses 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();
like image 30
m0skit0 Avatar answered Sep 10 '26 23:09

m0skit0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!