[英]Unexpected results of integer/double interaction - zero is returned after the division operation
I have an issue with the "documentLength" variable. 我对“ documentLength”变量有疑问。 When i input the paramenter into the method, it is 93. But for some reason it changes.
当我将参数输入到方法中时,它是93。但是由于某种原因,它会更改。 The input parameter is also an int variable.
输入参数也是一个int变量。
I call the class Word, and the method inside it occupationOfDocument from the class checkWord in main like this: 我将这个类称为Word,然后将其从main中的checkWord类中的职业中的方法称为:
double occupation = word.get(1).occupationOfDocument(documentLength); // line 83 in run-time below
occupationOfDocument looks like this 职业文档
public double occupationOfDocument(int documentLength){
int length = getLength(); //equals 8 when I run the program
int occurances = getOccurances(); //equals 3 when I run the program
System.out.println(documentLength); //Prints 93 as expected
double occupation = ((length * occurances)/documentLength)*100; //equals 0 somehow (probably division by)
return occupation; //expected around 25.8
}
This method returns "0", but if I change the code to this, 该方法返回“ 0”,但是如果我将代码更改为此,
private int documentLength = 0;
public double occupationOfDocument(int documentLength){
int length = getLength(); //equals 8 when I run the program
int occurances = getOccurances(); //equals 3 when I run the program
documentLength = this.documentLength;
System.out.println(documentLength); //Prints 0 somehow
double occupation = ((length * occurances)/documentLength)*100; //equals 0 somehow (probably divisjon by 0)
return occupation; //expected around 25.8
}
I get the following RUN-TIME-error: 我收到以下运行时错误:
Exception in thread "main" java.lang.ArithmeticException: / by zero at Word.occupationOfDocument(Word.java:44) at checkWord.main(checkWord.java:83)
Word.java:44 This is the line with math, where i declare and initialize occupation Word.java:44这是数学行,我在其中声明和初始化职业
I understand that it says I am dividing by 0. I just don't understand how it happens. 我知道它表示我被0除。我只是不知道它是如何发生的。
If i input a number instead of the variable "documentLength", the script does as expected 如果我输入数字而不是变量“ documentLength”,则脚本将按预期运行
Thanks for helping ;) 感谢您的帮助;)
IT student in need of help IT学生需要帮助
public double occupationOfDocument(int documentLength){
int length = getLength(); //equals 8 when I run the program
int occurances = getOccurances(); //equals 3 when I run the program
System.out.println(documentLength); //Prints 93 as expected
double occupation = ((length * occurances)/documentLength)*100; //equals 0 somehow (probably division by)
return occupation; //expected around 25.8
} }
Here, you have multiplied 8 by 3 and gotten 24, then you did integer division by 93 - and that gives you 0. 在这里,您将8乘以3并得到24,然后将整数除以93-得到0。
If you modify the line 如果您修改该行
double occupation = ((double) (length * occurances)/documentLength)*100;
that will produce the appropriate result. 将会产生适当的结果。 Learn more about type conversion and type widening to get more context.
了解有关类型转换和类型扩展以获得更多上下文的更多信息。
You are doing integer division in the first scenario, that means for example 4/3 = 1
and in your case 24/93 = 0
if you want to get it as a double you need to add a cast to the int values: 您在第一种情况下进行整数除法,这意味着例如
4/3 = 1
,在您的情况下为24/93 = 0
如果要使其为双24/93 = 0
则需要在int值上添加24/93 = 0
:
double occupation = (((double)(length * occurances))/((double)documentLength))*100;
Should be safe. 应该是安全的。 As for the second error you explicitly tell the program to
至于第二个错误,您明确告诉程序
documentLength = this.documentLength; //this.documentLength = 0 as you defined above
double occupation = ((length * occurances)/documentLength)*100;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.