划分两个整数在Java中给了我0或100? [英] Dividing two integers in Java gives me 0 or 100?

查看:112
本文介绍了划分两个整数在Java中给了我0或100?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算两个整数的和再乘以100,但它一直只给0或100。一个人可以帮我吗?

I'm trying to divide two integers and multiply by 100 but it keeps giving only 0 or 100. Can someone help me?

    int x= (a/b)*100;

如果一个是500和b是1000它会给我0.唯一一次它会给我100是,如果一个> = B。我该如何解决这个问题?

if a was 500 and b was 1000 it would give me 0. The only time it will give me 100 is if a>=b. How can I fix this?

感谢

推荐答案

你可以做的是迫使它划分 A B 双打是这样的:

What you could do is force it to divide a and b as doubles thus:

int x = (int)(((double)a/(double)b) * 100);

这篇关于划分两个整数在Java中给了我0或100?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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