Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于增量while循环确定百分比时逻辑错误导致的输出错误_Java_Loops_For Loop_Logic - Fatal编程技术网

Java 基于增量while循环确定百分比时逻辑错误导致的输出错误

Java 基于增量while循环确定百分比时逻辑错误导致的输出错误,java,loops,for-loop,logic,Java,Loops,For Loop,Logic,我创建了一个简单的类,它计算赢/输百分比,并返回必要的连续赢的数量,以根据给定的赢、输和期望赢/输值,从用户处获得给定输入的期望赢/输百分比。然而,在运行程序时,我注意到输出给出了一个非常大的数字,而不是应该是一个相对较小的数字,即所需的赢数。我不确定是什么错误造成了这个错误,但我确定它与howManyToWinLoss方法中的while循环有关,或者可能是我在tester类的输出中犯了一些错误。无论如何,它们都位于下面,我感谢您的帮助: public class WinLossCalculat

我创建了一个简单的类,它计算赢/输百分比,并返回必要的连续赢的数量,以根据给定的赢、输和期望赢/输值,从用户处获得给定输入的期望赢/输百分比。然而,在运行程序时,我注意到输出给出了一个非常大的数字,而不是应该是一个相对较小的数字,即所需的赢数。我不确定是什么错误造成了这个错误,但我确定它与howManyToWinLoss方法中的while循环有关,或者可能是我在tester类的输出中犯了一些错误。无论如何,它们都位于下面,我感谢您的帮助:

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;
    }
}

此外,我觉得我的tester类的输出部分有点难看,有没有人对输出部分中的代码的格式设置或清理有什么建议?

好的,显然这与我没有将for循环中的第一行按如下方式转换为double有关:winLoss=doublewin+x/doublewin+x+loss;我不知道它为什么坏了,但是,这就是它为什么坏了

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());
    }
}