[英]for-loop seems to only run once when it should run until certain value reached
我有一些代码可以显示正确的信息,但是循环仅重复一次,因此我需要重复直到达到最大值。
double x, v, max = .092 * Volt;
int t = 0, resistance = initialResistance, stop = 0;
System.out.print("\t");
for (int column = initialResistance -= 100; column < finalResistance; column += 100) {
System.out.print(resistance + " \t");
if (resistance < finalResistance) {
resistance += 100;
}
}
System.out.println("");
for (int i = 0; i <= t; i += 10) {
System.out.print("t:");
for (int j = initialResistance -= 100; j < resistance; j += 100) {
if (resistance < finalResistance) {
resistance += 100;
}
while (stop != 1)
{
resistance = initialResistance;
for (int r = initialResistance -= 100; r <= resistance; r += 100) {
if (resistance >= finalResistance)
{
resistance = finalResistance;
}
x = -(t / (resistance * capacitorValue));
v = Volt * (1 - Math.pow(BASE, x));
if (v <= max)
System.out.print("\t"+ String.format("%.2f", v));
else
System.out.print("");
if (r >= resistance && v > max)
{
stop = 1;
}
resistance += 100;
}
t += 10;
System.out.println("");
}
}
}
500600700
t:0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.92 0.77 0.67 0.67 0.67
t:t:t:
是当前显示,当500和700是初始和最终阻力,而.25是capacticorValue
因此它可以正确显示,但需要重复更多,我需要删除多余的0.00和.67并弄清楚为什么第二行不显示“ t:”
我认为这是因为initialResitance - 100
比resistance
小100,并且如果j
(或r
)每次增加100时initialResitance - 100
100 / 100 = 1
在j
循环的第一次迭代期间,内部while(stop != 1)
循环被stop = 1
中断。 当第二个j
迭代开始时,此标志变量保持设置状态。 由于stop
仍为1,因此再也不会进入while
循环。
请注意,在while循环后将stop重置为0并没有帮助。 该程序将无限期运行,因为t
在内部循环中递增,因此for(int i
循环永远不会停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.