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
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.
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.
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 “ ...
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 { 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With