【发布时间】:2019-11-05 18:43:48
【问题描述】:
所以这是重要的代码的整体
int dom = Integer.parseInt(Domin.getText());
double fraction = Integer.parseInt(Numer.getText())/Integer.parseInt(Domin.getText());
String currentlow = "";
System.out.println("TEST");
for (int i = 0; i >= dom;i++){ //ok the problem wasn't that it was > dom instead of >= dom
System.out.println("dummy"); //this doesn't print and it would print every time if it was running the for loop.
if((num % i == 0)&&(dom % i == 0)){ //this just = checks to see that there's no remainder (like 5/5)
System.out.println("true"); //this for some reason never triggers even though i'm testing with 5/25
if ((num/i)/(dom/i) == fraction){ //this is a dummy check to make sure it doesn't round improperly
currentlow = String.valueOf(num/i) + "/" + String.valueOf(i); //this sets the value but isn't the problem since the console never says "true"
System.out.println(currentlow); //nother dummy check
}
}
}
如果需要,可以编辑 cmets,但基本上 for 循环应该会导致它除以小于支配者的所有数字,但它甚至从未打开 for 循环(它不打印“dummy ” 或“真”,当它应该在我的测试中打印 24 次时)无法弄清楚为什么
【问题讨论】:
-
您正在检查 0 >= dom。 dom 是负数吗?
-
我猜是
dom/i调用,因为它是integer division。 -
i >= dom—Domin.getText()的文本是什么,因此dom的值是什么?您从i = 0开始并检查i是否大于或等于 到dom所以,除非dom是一个负数,否则它永远不会循环。 -
它不会按预期工作,要么 i 永远 >= dom 要么它总是因为你在检查大于时增加 i
-
此外,检查浮点值之间的精确相等性并不是一个好主意。