the main purpose of this application is to ask the user for head or tail and the output the numbers of guesses + the percentage of correct. Im having trouble with finding the percentage of correct. please help me fix this problem

public class FlipCoin 
  public static void main(String[] args)
  Scanner in = new Scanner(System.in);
  Random r = new Random();
  int user;
  int answer = r.nextInt(2)+1;
  int quit = -1;
  int Guess = 0;
  int percent;
  do {
    System.out.println("1 for head and 2 for tails:");
    user = in.nextInt();
    Guess += 1;
    System.out.println("Type in 1 for heads and Type 2 for Tails.");
    System.out.println("Press" +" "+ (quit) +" "+ "to Quit.");
    answer = r.nextInt(2)+1;
     int HEAD = 1;
     int TAIL = 2;
    if (user == answer)
    System.out.println("You win!");
    else if (user != answer)
      System.out.println("Try again!");
      percent = ((HEAD/TAIL)*100);
  }while (user != -1);
    System.out.println("You made" +" "+ Guesses +" "+ "guess");
      System.out.println("You Quit!");
5 Years
Discussion Span
Last Post by hfx642

What output are you expecting, and what are you actually getting?

On quick inspection, you're using integers for HEAD and TAIL , which is probably making it consistently display '0 percent'. Try casting one of them to a floating-point value, for example: (((double)HEAD/TAIL)*100)


i want to output the total percent of the correction

The first thing you're missing is that somewhere you need to count the number of correct guesses and the total number of guesses.


(HEAD/TOTAL)*100 // will give you the percentage that HEADs comes up
(TAIL/TOTAL)*100 // will give you the percentage that TAILs comes up
And percent should be a double.
Add this to gusano79's recommendation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.