Java 没有 运行 这个 for 循环
Java doesn't run this for loop
所以这里是重要的代码的完整性
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
}
}
}
如果你愿意,可以编辑注释,但基本上 for 循环应该使它除以所有小于支配符的数字,但它甚至从未打开 for 循环(它不打印"dummy" 或 "true" 曾经,在我的测试中它应该打印 24 次)不知道为什么
您的 for 循环条件看起来不对。您的起始条件是 i = 0
,但您的结束条件是 i >= dom
。如果 dom 是任何大于 0 的整数,那么 for 循环将永远不会执行,因为 i 不能同时为 0 和大于或等于大于 0 的数字。我想你的意思是说 i <= dom
?
你这里有一些缺陷。首先,正如其他人指出的那样,这是错误的:
for (int i = 0; i >= dom;i++)
如果 dom
是 0
(在这种情况下,它会 运行 恰好一次)。如果 dom
大于 0,则 i
永远不会大于或等于它。另一方面,如果 dom
是负数,i
将 总是 大于它,因为你在每次迭代中都在做 i++
。
其次,你做的除法不对。结果如下:
double x = Integer.parseInt("3") / Integer.parseInt("4");
System.out.print(x); // Prints 0.0
实际上是 0.0
因为它在做整数除法。然而,这样做的结果:
double x = (double)Integer.parseInt("3") / Integer.parseInt("4");
System.out.print(x); // Prints 0.75
是预期的 0.75
.
您在代码中的多个位置执行此操作。
所以这里是重要的代码的完整性
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
}
}
}
如果你愿意,可以编辑注释,但基本上 for 循环应该使它除以所有小于支配符的数字,但它甚至从未打开 for 循环(它不打印"dummy" 或 "true" 曾经,在我的测试中它应该打印 24 次)不知道为什么
您的 for 循环条件看起来不对。您的起始条件是 i = 0
,但您的结束条件是 i >= dom
。如果 dom 是任何大于 0 的整数,那么 for 循环将永远不会执行,因为 i 不能同时为 0 和大于或等于大于 0 的数字。我想你的意思是说 i <= dom
?
你这里有一些缺陷。首先,正如其他人指出的那样,这是错误的:
for (int i = 0; i >= dom;i++)
如果 dom
是 0
(在这种情况下,它会 运行 恰好一次)。如果 dom
大于 0,则 i
永远不会大于或等于它。另一方面,如果 dom
是负数,i
将 总是 大于它,因为你在每次迭代中都在做 i++
。
其次,你做的除法不对。结果如下:
double x = Integer.parseInt("3") / Integer.parseInt("4");
System.out.print(x); // Prints 0.0
实际上是 0.0
因为它在做整数除法。然而,这样做的结果:
double x = (double)Integer.parseInt("3") / Integer.parseInt("4");
System.out.print(x); // Prints 0.75
是预期的 0.75
.
您在代码中的多个位置执行此操作。