Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Do/While validation?

Tags:

java

I'm currently taking an intro to Java class and I'd really like to improve but I'm struggling to complete this assignment. The requirements for the assignment was to

  1. Implement a loop that allows the user to continue to play the game by typing yes.
  2. Keep track of the users: Wins, Losses and Games Played using incrementing variables
  3. When the user no longer wishes to continue, print their results displaying the tracked variables: Wins, Losses and Games Played
  4. Implement an input validation loop to ensure the user enters correct input (h, t, H, T)

I believe I've done all but the last one, I've tried numerous times to use do and while loops but the closest I've gotten was the wrong input looping while the correct input would bypass all other if/while/else statements. If at all possible I'd greatly appreciate someone taking a look at my code and explaining what I could do better, and how I could complete or approach the last requirement.

Thank you!

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String input, inputUpper;
    char userGuess;
    char coinFlip;
    int randNum;
    int wins = 0;
    int losses = 0;
    int total = 0;
    String choice = "yes";

    do {
        System.out.print("I will flip a coin guess 'H' for heads or 'T' for 
        tails --> ");
        input = scan.nextLine();

        inputUpper = input.toUpperCase();

        userGuess = inputUpper.charAt(0);
        randNum = (int) (Math.random() * 2);

        if(randNum == 0)
        {
            coinFlip = 'H';
        }
        else
        {
            coinFlip = 'T';
        }

        System.out.println("\nYou picked " + userGuess + 
        " and the coin flip was " + coinFlip + " so ...");

        if(userGuess == coinFlip)
        {
            System.out.println("You win!");
            wins ++;
            total ++;
        }
        else
        {
            System.out.println("Better luck next time ...");
            losses ++;
            total ++;
        }
        System.out.println("Do you want to continue(yes/no)?");
        choice=scan.nextLine();
    } while(choice.equalsIgnoreCase("yes"));
    System.out.println("Thank you for playing.");
    System.out.println("You guessed correctly this many times: " +wins);
    System.out.println("You guessed incorrectly this many times: " +losses);
    System.out.println("During this session you've played this many games: " +total);
    }
}

I expect the program to require T/t or H/h in order to continue, and if the user inputs an incorrect letter or number it'll ask them to input t or h.

like image 770
JavaNovice Avatar asked Mar 25 '19 05:03

JavaNovice


People also ask

Which loop is best for validation user?

Often it is necessary to validate data input by the user, and repeat the request for the data in the case where the input of the user is not valid. This can be done by using a do loop.

What is an example of input validation?

For example, validating that an input value is a credit card number may involve validating that the input value contains only numbers, is between 13 and 16 digits long, and passes the business logic check of correctly passing the Luhn formula (the formula for calculating the validity of a number based on the last “ ...

How is the while loop used for input validation?

To validate input in a while loop: Use a try/except or an if/else statement to validate the input. If the input is invalid, use a continue statement to continue to the next iteration. If the input is valid, use a break statement to break out of the loop.

Do loops in C?

do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.


1 Answers

Here's a simple way to validate the input:

do {
    System.out.print("I will flip a coin guess 'H' for heads or 'T' for tails --> ");
    input = scan.nextLine();
    inputUpper = input.toUpperCase();
} while (!inputUpper.equals("T") && !inputUpper.equals("F"));

You could do the same thing at the end for "yes"/"no".

I think you did a pretty good job with this.

like image 164
CryptoFool Avatar answered Oct 21 '22 12:10

CryptoFool