【发布时间】:2015-11-17 21:39:45
【问题描述】:
我有一些显示正确信息的代码,但循环只重复一次,我需要重复直到达到最大值。
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("");
}
}
}
500 600 700
吨: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 是初始和最终阻力并且 0.25 是电容值时的当前显示
所以它显示正确但需要重复更多,我需要摆脱额外的 0.00 和 .67 并弄清楚为什么它没有在第二行显示“t:”
【问题讨论】:
-
"[...] 但循环只重复一次。[...]" 您有多个循环。你说的是哪个循环?另外,请正确格式化您的代码,特别是如果您有嵌套结构。
-
我不确定哪个循环有问题,我怎样才能更好地格式化它?
-
看看你的缩进。最里面的
while里面有些部分有点乱。 -
对吗,你总是在for循环的开头递减
initialResistance,因为你使用了运算符-=而不是简单的减法。 -
我改变了它,但它没有做任何改变代码
标签: java loops for-loop nested nested-loops