简体   繁体   English

乘法时得到错误的值

[英]Getting wrong value when multiplying

I am writing an Android app and I have an algorithm to calculate some score out of several variables but when computing, I get wrong answer: 我正在编写一个Android应用,并且我有一种算法可以从多个变量中计算出一些分数,但是在计算时,我得到了错误的答案:

I get 10300 when I set mv, ptv, txtv to 10 whereas I should get 100. When I set it to 1, I get 300 as answer. 当我将mv,ptv,txtv设置为10时我得到10300,而我应该得到100。当我将其设置为1时,我得到300作为答案。

int f;
f = (((mv*ptv*txtv)/10^3)*100);
int finalScr = f;
TextView scoreView = (TextView)findViewById(R.id.textView3);
scoreView.setText(Integer.toString(finalScr));

All the variables are integers and the maximum value of all the variables is 10 and minimum is 1. 所有变量均为整数,所有变量的最大值为10,最小值为1。

Please help.. I don't think I am mathematically wrong :P 请帮助..我不认为我在数学上是错误的:P

^ is not the power operator in Java . ^不是Java的幂运算符。 It's an XOR operator. 这是一个XOR运算符。

You should use Math.pow(10, 3) for that: 您应该为此使用Math.pow(10, 3)

f = (int)(((mv*ptv*txtv)/Math.pow(10, 3))*100);

Typecast to int is needed, as the result of Math.pow is double type. 需要将类型转换为int ,因为Math.pow的结果是double类型。

^ is not the exponential operator. ^不是指数运算符。 Its an xor operator. 它是一个xor运算符。 You need to make it 你需要做到

f = (((mv*ptv*txtv)/(10*10*10))*100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM