我不能正确划分两个数字 [英] I can not divide two numbers correctly

查看:110
本文介绍了我不能正确划分两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int percent = (score/numberOfQuestions)*100;
progressText.setText(score+"/"+numberOfQuestions+"  "+percent+"%");

返回0%,不管我累了。我试图强制转换为int,双,浮法

returns 0% no matter what I tired. I tried casting it to int, double, float

为什么它返回0%,对于一些喜欢得分= 5 numberOfQuestions = 8?

Why does it return 0% for a number like score=5 numberOfQuestions=8?

推荐答案

问题是,计算两个整数给你结果的整数部分。因此,(得分/ numberOfQuestions)将始终为0。
你应该做的是

The problem is that divide two integers gives you the integer part of the result. So, (score/numberOfQuestions) will be always 0.
What you should do is

int percent = (score * 100 / numberOfQuestions);

然后 * 100 将首先执行,那么鸿沟会给你正确的结果。

Then the *100 will be executed first, then the divide will give you correct result.

这篇关于我不能正确划分两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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