Output error resulting from logic error when determining percentage based on incremental while loop

StackOverflow https://stackoverflow.com/questions/23164912

  •  06-07-2023
  •  | 
  •  

Question

I have created a simple class which calculates the win/loss percentage and returns the amount of necessary consecutive wins to obtain the desired win/loss percentage of the given input from the user, based on the given values of wins, loss, and desired win/loss. However, whilst running the program, I notice the output gives an exceedingly large number in the place of what should be a relatively small number (the number of wins needed). I am not certain what error I have made that created this, but I'm certain it's relative to the while loop located in my howManyToWinLoss() method, or perhaps some error I've made in the tester class's output. At any rate, they are both located below, and I appreciate the assistance:

public class WinLossCalculator
{
    private int win;
    private int loss;
    private int totalGames;
    private double desiredWinLoss;
    private double winLoss;
    private int gamesToDWL;

    public WinLossCalculator(int w, int l, double dwl)
    {
        win = w;
        loss = l;
        totalGames = w + l;
        desiredWinLoss = dwl;
    }

    public double winPercent()
    {
        return winLoss = (double)win/(double)totalGames;
    }

    public double lossPercent()
    {
        return (double)loss/(double)totalGames;
    }

    public int howManyToWinloss()
    {
        int x = 0;
        while(winLoss < desiredWinLoss)
        {
            winLoss = (win+x)/(win+x+loss);
            if(winLoss < desiredWinLoss)
            {
                x++;
            }
        }
        return gamesToDWL = x;
    }
}

and

import java.util.*;
public class WinLossTester
{
    public static void main(String []args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the amount of matches you've won: ");
        int wins = in.nextInt();

        System.out.print("\nEnter the amount of matches you've lost: ");
        int losses = in.nextInt();

        System.out.print("\nEnter your desired match win percentage (eg. 75.4): ");
        double desiredWLP = in.nextDouble();

        System.out.println();

        WinLossCalculator one = new WinLossCalculator(wins, losses, (desiredWLP / 100));

        one.winPercent();
        one.howManyToWinloss();

        System.out.printf("Win percentage: %6.1f", (one.winPercent()*100));
        System.out.print("%");
        System.out.printf("\nLoss percentage: %5.1f", (one.lossPercent()*100));
        System.out.print("%");
        System.out.println("\nVictories required to reach desired W/L percent (without loss): " + one.howManyToWinloss());
    }
}

Additionally--I feel as though my tester class's output section is a little ugly, might anyone have any suggestions concerning formatting or cleaning up the code in the output section?

Was it helpful?

Solution

Ok, apparently it has to do with me not casting the first line in my for loop to double like this winLoss = (double)(win+x)/(double)(win+x+loss); I don't know why that broke it, but, that's why it was broken.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top