计算百分比"x/y * 100".总是结果为0? [英] Calculating percent "x/y * 100" always results in 0?

查看:137
本文介绍了计算百分比"x/y * 100".总是结果为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的作业中,我必须制作一个简单的Craps版本,由于某些原因,即使两个变量都不为0,百分比作业也总是产生0,这是代码.

In my assignment i have to make a simple version of Craps, for some reason the percentage assignments always produce 0 even when both variables are non 0, here is the code.

import java.util.Random;

标题,请注意变量

public class Craps {
private int die1, die2,myRoll ,myBet,point,myWins,myLosses;
private double winPercent,lossPercent;
private Random r = new Random();

只需滚动两个模具,然后生产两个模具即可.

Just rolls two dies and produces their some.

public int roll(){
    die1 = r.nextInt(6)+1;
    die2 = r.nextInt(6)+1;
    return(die1 + die2);
}

Play方法,这只会在游戏中循环.

The Play method, this just loops through the game.

public void play(){
    myRoll = roll();
    point = 0;

    if(myRoll == 2 ||myRoll == 3 || myRoll == 12){
        System.out.println("You lose!");
        myLosses++;
    }else if(myRoll == 7 || myRoll == 11){
        System.out.println("You win!");
        myWins++;
    }else{
        point = myRoll;
        do {
            myRoll = roll();
        }while(myRoll != 7 && myRoll != point);
        if(myRoll == point){
            System.out.println("You win!");
            myWins++;
        }else{
            System.out.println("You lose!");
            myLosses++;
        }
    }
}

这是bug所在的地方,这是测试人员的方法.

This is where the bug is, this is the tester method.

public void tester(int howMany){
    int i = 0;
    while(i < howMany){
        play();
        i++;
    }

这些任务声明中的

bug就在这里

bug is right here in these assignments statements

    winPercent = myWins/i * 100;
    lossPercent = myLosses/i* 100;
    System.out.println("program ran "+i+" times "+winPercent+"% wins "+ lossPercent+"% losses with "+myWins+" wins and "+myLosses+" losses");



}

}

推荐答案

可能是因为您正在执行整数除法,并且分母大于分子.因此该值将为零.

Probably because you are doing an integer division, and denominator is greater than numerator. So the value will be zero.

您可以更改此分配:-

winPercent = myWins/i * 100;
lossPercent = myLosses/i* 100;

至:-

winPercent = myWins * 100.0/i;
lossPercent = myLosses * 100.0/i;

有两点要考虑:-

  • 通过强制转换使您的除法成为除法的浮点 您的分子到float:-

  • Either make your division a floating point of division, by casting your numerator to float: -

winPercent = (float)(myWins)/i * 100;

  • 此外,在整数除法的情况下,它是关联性的问题 和优势.由于*/具有相同的优势,并且 具有从左到右的关联性.因此,myWins / i将是 首先评估.因此,很有可能该价值可以 0.

  • Also, in case of integer division, its the matter of associativity and precendence. Since * and / have same precendence, and have left-to-right associativity. So, myWins / i will be evaluated first. So, there is bright chances that that value can be 0.

    因此,只需更改*/的顺序以使*发生 首先.

    So, just change the order of * and / to make the * happen first.

    注意,我在乘法中使用了100.0.因为乘以100会再次导致截断.

    Note, I have used 100.0 in multiplication. Because multiplying by 100 will again result in Truncation.

    这篇关于计算百分比"x/y * 100".总是结果为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆