Home > Net >  Do while and while loop result in an infinite loop even when the condition is false
Do while and while loop result in an infinite loop even when the condition is false

Time:04-26

So I am creating a blackjack game as part of a project in my college and I need a part of code to check the player input to see if he inputs H for hit or F for fold.My original code was this:

String playerinput;
do {
System.out.println(playernames[j] " (H/F)");
playerinput=input.next();
} while (!((playerinput.equalsIgnoreCase("H"))||(playerinput.equalsIgnoreCase("F"))));

The problem is that when I run it it gets stuck even though I input H or F accordingly.I decided to reshape it to this in order to try and debug it:

int temp=0;
while (temp==0) {
    System.out.println( playernames[j]  " (H/F)");
    playerinput=input.next();
    if (playerinput.equalsIgnoreCase("H")){
        temp=1;
        System.out.println("if 1");
        break;
    }
    else if (playerinput.equalsIgnoreCase("F")) {
        temp=1;
        System.out.println("if 2");
        break;
    }
    System.out.println("Here");
}

This code seems logical and does not have any syntax problems,yet it results in an infinite loop.I just want to check if the user has input H or F and if not,then he gets stuck in the loop.However,when I run it,it results in this:

Nick (H/F)
H
if 1
Nick (H/F)
F
if 2
Nick (H/F)
J
Here
Nick (H/F)

I have also tried using a boolean temp and it still did the same thing.

CodePudding user response:

Since we can't see the code around your while look, my guess is that you have another while loop outside of the that you've shown and that you're just looping inside of that while loop.

Here is an example of what I think is happening in your code.

package whileloop;

import java.util.Scanner;

public class BreakStatement {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("start");

        boolean outer = true;

        while (outer) {

            System.out.println("outer start");

            boolean test = true;
            while (test) {

                System.out.println("test");

                String input = scanner.next();

                if (input.equalsIgnoreCase("H")) {

                    System.out.println("hold");
                    test = false;
                    break;
                } else if (input.equalsIgnoreCase("F")) {

                    System.out.println("fold");
                    test = false;
                }
            }

            System.out.println("outer end");
        }

        System.out.println("end");
    }
}

Output:

start
outer start
test
h
hold
outer end
outer start
test
f
fold
outer end
outer start
test

CodePudding user response:

Tried this and it seemed to work fine.

public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String playerinput;
        String playerName = "Nick";
        do {
            System.out.println(playerName " (H/F)");
            playerinput=input.next();
        } while (!((playerinput.equalsIgnoreCase("H"))||(playerinput.equalsIgnoreCase("F"))));
    }

Output:

Nick (H/F)
H

Process finished with exit code 0

Haven't really changed much but wanted to asnwer so you could see if there is any difference.

  • Related