Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I need to revise my java code for my assignment.

For each round, the two players take turns: To make it clear, the human chooses heads or tails, the computer picks randomly.The game loop consists of a player selecting appropriate input (heads or tails) via an Input Dialog,I am having issues for this part.


Could you help me for the missing part? Please see my code at below:

What I have tried:

<pre>import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class CountingHeadTail {
    private static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {
		String headsOrTailsGuess = "";
		String guessRight;
		int correctCount =0;
		int  numberOfFlips =0;
		int randCoin;
		int userCoin = -1;
		final int HEADS = 0;
		final int TAILS = 1;
		double percentCorrect = 0.0;
		Random rand = new Random();
		
		
		do {
			System.out.println("Guess which will have more: heads or tails?");
			headsOrTailsGuess = scanner.nextLine().toLowerCase();
			if (headsOrTailsGuess.equals("heads")) {
				userCoin = HEADS;
			} else if (headsOrTailsGuess.equals("tails")) {
				userCoin = TAILS;
			} else {
				headsOrTailsGuess = "";
			}		
		} while (headsOrTailsGuess == "");

		
		do {
			System.out.println("How many times shall we flip a coin?");
			try {
				numberOfFlips = scanner.nextInt();
			} catch (InputMismatchException exception) {
				numberOfFlips = -1;
				System.out.println("Please enter digits only, greater than 0");
			}
			scanner.nextLine();
		} while (numberOfFlips <= 0);
		
		for ( int i = 1; i<=numberOfFlips; i++) {
			randCoin = rand.nextInt(2);
			guessRight = "";
			switch(randCoin) {
				case HEADS:	
					if (userCoin == HEADS) {
						correctCount += 1;
						guessRight = "*";
					}
					System.out.println(i+ " heads" + guessRight);
					break;
				case TAILS:	
					if (userCoin == TAILS) {
						correctCount += 1;
						guessRight = "*";
					}
					System.out.println(i+ " tails" + guessRight);
					break;
				default:
					System.out.println("should never get here randCoin=" + randCoin);
			}
			
		}
		
		System.out.println("\nYour guess, "+ headsOrTailsGuess+", came up "+correctCount+
				" time" + (correctCount != 1 ? "s." : "."));
		
		percentCorrect = (double)correctCount/numberOfFlips*100.0;
		System.out.println("That's "+ Math.round(percentCorrect) +"%. ");

	}

}
Posted
Updated 8-Jul-21 1:18am
v4
Comments
Richard MacCutchan 1-Jul-21 4:38am    
What is the missing part, and what is your problem in writing it?
Richard MacCutchan 8-Jul-21 7:41am    
You have edited your question and now there is even less useful information.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
How can I add some additional java code to finish my assignment?

We can't do anything for you because you forgot to tell what is your assignment.
The 1 line of code will not help us either.
Reread your question, it must explain what you talk about.

We are not NSA, we can't read your screen, your HDD or your mind.

Asking questions is a skill[^]
Some guidelines for posting questions in the forums[^]
 
Share this answer
 
Comments
Richard Deeming 8-Jul-21 6:34am    
The OP edited their question to remove all useful content, presumably to try to disguise their attempt at cheating.
OriginalGriff 8-Jul-21 8:16am    
And it would appear that somebody rolled it back...

For once I don't think it was me ... but I would have! :D
Richard Deeming 8-Jul-21 8:17am    
No, that was me. :)
OriginalGriff 8-Jul-21 8:31am    
Take a virtual 5! :D
Patrice T 8-Jul-21 9:31am    
Agreed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900