1

I trying convert do-while to while loop and program even compile, but nothing happens. Original do-while loop code:

import java.util.Scanner;

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

        String coin, again;
        int flip, streak = 0;

        do
        {
            flip = 1 + (int)(Math.random()*2);

            if ( flip == 1 )
                coin = "HEADS";
            else
                coin = "TAILS";

            System.out.println( "You flip a coin and it is... " + coin );

            if ( flip == 1 )
            {
                streak++;
                System.out.println( "\tThat's " + streak + " in a row...." );
                System.out.print( "\tWould you like to flip again (y/n)? " );
                again = keyboard.next();
            }
            else
            {
                streak = 0;
                again = "n";
            }
        } while ( again.equals("y") );

        System.out.println( "Final score: " + streak );
    }
}

How it looks like after I trying to do while loop from do-while loop:

import java.util.Scanner;

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

        String coin, again;
        int flip, streak = 0;
        again = keyboard.next();
        //flip = 1 + (int) (Math.random()*2);

        while ( again.equals("y") )
        {
            flip = 1 + (int) (Math.random()*2);
            if ( flip == 1)
            {
                coin = "HEADS";
            }
            else
            {
                coin = "TAILS";
            System.out.println( "You flip a coin and it is..." + coin );
            }
            if ( flip == 1)
            {
                streak++;
                System.out.println("\tThat's " + streak + " in a row....");
                System.out.print( "\tWould you like to flip again (y/n)? ");
                //again = keyboard.next();
            }
            else
            {
                streak = 0;
                again = "n";
            }
            System.out.println( "Final score: " + streak);
        }
    }
}

Code compile without problem but I see blank output after running it.

2
  • 2
    Do-While loops are guaranteed to run at least once, while loops are not. You get a blank output because you are waiting for keyboard input on line 11 Commented Nov 6, 2015 at 9:46
  • You can't. do-whileloops and while loops are non-interchangeable, at least without an extra Boolean variable. Why are you even trying? Commented Nov 6, 2015 at 9:57

2 Answers 2

2

That's because you never initialize again variable to y, so it never matches condition to enter while

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

    String coin, again = "y";
    int flip, streak = 0;
    // flip = 1 + (int) (Math.random()*2);

    while (again.equals("y")) {
        flip = 1 + (int) (Math.random() * 2);
        if (flip == 1) {
            coin = "HEADS";
        } else {
            coin = "TAILS";
            System.out.println("You flip a coin and it is..." + coin);
        }
        if (flip == 1) {
            streak++;
            System.out.println("\tThat's " + streak + " in a row....");
            System.out.print("\tWould you like to flip again (y/n)? ");
            again = keyboard.next();
            // again = keyboard.next();
        } else {
            streak = 0;
            again = "n";
        }
    }
    System.out.println("Final score: " + streak);
    keyboard.close();
}

OUTPUT:

That's 1 in a row....
Would you like to flip again (y/n)? y
You flip a coin and it is...TAILS
Final score: 0
Sign up to request clarification or add additional context in comments.

Comments

1

Initialize your again with "y":

String coin, again;
int flip, streak = 0;
again = "y";
...

and ask for keyboard entry in your while loop:

if ( flip == 1)
{
    streak++;
    System.out.println("\tThat's " + streak + " in a row....");
    System.out.print( "\tWould you like to flip again (y/n)? ");
    again = keyboard.next();
}

2 Comments

Thanks that rocks! Is I understand correctly that if again = keyboard.next(); is commented here, then program always type "y" here for me automatically?
Well it won't change again at all so you'll run into an infinate loop!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.