0

I am writing a coin toss program. Basically, the user is asked how many games they want to play. Then, the while loop runs the game which asks the user to input a several coin tosses separated by spaces (heads or tails ex. H H H T H). Then the program converts the user input string into an array and the for loop goes through the array and checks for heads (capital H) and it is stored in the heads variable. The the score is calculated by diving the number of heads in the array by the length of the array and multiplies it by 100 to make a percentage. If the score is 50% or above, the player wins. If below 50%, the player loses. Two bugs I found were the while loop already runs once before i put in any input. Also the score seems to be calculated incorrectly where it either always returns 0.0% or 100.0%. I think it has something to do with the inputArray.length but I cannot tell. Thank you.

import java.util.Scanner;
public class HeadTailGenerator {

public static void main (String []args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("How many games?");
    int games = scanner.nextInt();
    int count = 0;
    int heads =0;

    while (count<games){
        System.out.print("Enter your flips for game "+ count+": ");
        String input = scanner.nextLine();
        String [] inputArray = input.split("\\s+");
        for (int i = 0; i <inputArray.length-1; i++){

            if (inputArray[i].equals("H")){
                heads++;
            }

        }//exit for loop

        double score = (heads/(inputArray.length)*100);

        if (score >= 50.0){
            System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You win!");
        }
        else {
            System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You lose!");
        }

        count++;
    }
}
}
  • Take a look at this, might be the same issue with `nextInt()`; https://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input – crimson589 Jan 29 '20 at 03:50
  • Also, please print the contents of inputArray to check what values are there after splitting. – PiNaKa30 Jan 29 '20 at 03:54

2 Answers2

1

Adding a few prints would help you to debug all issues.

The first problem is that the nextInt() doesn't capture the line break used to enter the number of games so you can add scanner.nextLine(); after that to prevent the game from ending right away.

The second problem is the condition in your for loop. Remove the -1 to loop over all the elements in the array.

The third problem is that in the calculation to get the score you are dividing an integer by a number that is always greater or equal to that integer, so the decimals are truncated or the result is 1. You can either cast the integer to double in the operation or declare heads as double or alternatively you can multiply by 100 before dividing if you don't care about the decimals of the percent result.

import java.util.Scanner;
public class Main {
    public static void main (String []args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many games? ");
        int games = scanner.nextInt();
        System.out.println("Games: "+ games);
        scanner.nextLine();
        int count = 0;
        int heads = 0;

        while (count<games){
            System.out.print("Enter your flips for game "+ count+": ");
            String input = scanner.nextLine();
            String [] inputArray = input.split("\\s+");
            for (int i = 0; i <inputArray.length; i++){
                System.out.println("Input["+ i +"]: "+ inputArray[i]);
                if (inputArray[i].equals("H")){
                    heads++;
                    System.out.println("Heads count: "+ heads);
                }

            }//exit for loop

            double score = ((double)heads/(inputArray.length)*100);

            if (score >= 50.0)
                System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You win!");
            else 
                System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You lose!");

            count++;
        }
    }
}
0

I ran across this not too long ago. The first problem, why while loop runs once before accepting input from user is because of the way scanner handles reading nextInt(). Scanner has just read the next integer value it came to but has not gotten to the end of the line. Therefore, when you call scanner.nextLine() inside while loop it actually is just reading the end of the first line of input and that is why you get the zeros there.

The second problem, always getting zero, has to do with the data type you are using. Remember, an int / int = int. So, when you divide 3/4 = 0 because int gets truncated. It doesn't matter that you are storing in a double, it is the integer division that is getting it wrong. If you simply change the heads to float it will correct that.

You can find out more about scanner here.

stujar
  • 1
  • 2