任何想法为什么在用户指定的数量之后For循环不会被踢出?用户可以输入的最大金额为100。
class MakeTables
{
private static final int MAX_NUMBER_TABLES = 100;
public static void main(String[] args)
{
Table[] tables = new Table[MAX_NUMBER_TABLES];
int i = Integer.parseInt(JOptionPane.showInputDialog("How many tables would you like to create?"));
for (i = 0; i < tables.length; i++)
{
tables[i] = new Table();
tables[i].setHeight(Double.parseDouble(JOptionPane.showInputDialog("Enter height:")));
tables[i].setWeight(Double.parseDouble(JOptionPane.showInputDialog("Enter weight:")));
tables[i].setColor(JOptionPane.showInputDialog("Enter color:"));
tables[i].setNumberOfLegs(Integer.parseInt(JOptionPane.showInputDialog("Enter number of legs:")));
if (tables[i] != null)
JOptionPane.showMessageDialog(null,(tables[i].toString()));
} // end for
} // end main
} // end class
答案 0 :(得分:0)
你可以尝试:
int j = Integer.parseInt(JOptionPane.showInputDialog("How many tables would you like to create?"));
for (i = 0; i < j; i++)
{...
}
答案 1 :(得分:0)
问题
您要求用户输入并将其存储在i
int i = Integer.parseInt(JOptionPane.showInputDialog("How many tables would
+ you like to create?"));
然后,您将i
的值重置为0
。
for (i = 0; i < tables.length; i++)
<强>解决方案强>
int input = Integer.parseInt(JOptionPane.showInputDialog("How many tables would
+ you like to create?"));
for (i = 0; i < input; i++)
答案 2 :(得分:0)
启动for循环时,将用户输入的值(存储在i
中)重置为0,for循环将从0开始执行,直到不符合设置为止条件(i < tables.length
)了,所以无论如何它从0到100。我建议你在for循环中使用不同的迭代器,这样就不会覆盖你的用户输入。此外,你应该重新审视你的循环条件(考虑一下你真正想要的东西!)。