gpt4 book ai didi

Java 不运行这个 for 循环

转载 作者:行者123 更新时间:2023-12-02 09:20:40 28 4
gpt4 key购买 nike

所以这是重要的代码的完整性

       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

关于Java 不运行这个 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58717608/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com