【发布时间】:2020-04-14 09:11:46
【问题描述】:
我知道我对此有点愚蠢,但我想我要休息几分钟,然后问这个问题。我正在尝试向输入框添加倒计时功能。问题在于,当我真正尝试增加一个在循环本身内部调用的变量时,它会执行输入量的平方次数。我确信它会来找我,但让别人的观点会有所帮助。
String propertyNumber = JOptionPane.showInputDialog("Enter Numer of Properties...");
int propNumber = Integer.parseInt(propertyNumber);
numOfProperties = new float[propNumber];
for(int i= 0; i < propNumber; i++) {
for(int a = propNumber; a >= 0; a--) {
String propertyVal = JOptionPane.showInputDialog("Enter each property value for the " + propNumber + " Properties you listed. You have " + a + " inputs left.");
numOfProperties[i] = Float.parseFloat(propertyVal); //takes property value info and stores them inside the property number array
}
}
【问题讨论】:
-
终止对我来说很好。不知道为什么你有外循环。你调试过这段代码吗?
-
您的循环不是无限的,您只需提示
propNumber属性propNumber² 次。为什么要嵌套循环?int a = propNumber - i;和一个循环(这是我认为你想要的)。 -
刚刚意识到我的问题是错误的。我的问题是 - 每当我输入,比如 3,属性。它将运行循环 9 次。我认为这是因为我在循环中计算了两次变量“propNumber”。