Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在用户输入特定数量后,我的For循环会继续?_Java_For Loop - Fatal编程技术网

Java 为什么在用户输入特定数量后,我的For循环会继续?

Java 为什么在用户输入特定数量后,我的For循环会继续?,java,for-loop,Java,For Loop,你知道为什么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.parseI

你知道为什么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
类生成表
{
专用静态最终整数最大数表=100;
公共静态void main(字符串[]args)
{
Table[]tables=新表[MAX_NUMBER_tables];
int i=Integer.parseInt(JOptionPane.showInputDialog(“您想创建多少个表?”);
对于(i=0;i
您可以尝试:

int j = Integer.parseInt(JOptionPane.showInputDialog("How many tables would you like to create?"));
for (i = 0; i < j; i++)
{...
}
int j=Integer.parseInt(JOptionPane.showInputDialog(“您要创建多少个表?”);
对于(i=0;i
问题

您要求用户输入并将其存储在
i

int i = Integer.parseInt(JOptionPane.showInputDialog("How many tables would 
                                                     + you like to create?"));
然后将
i
的值重置为
0

for (i = 0; i < tables.length; i++)
for(i=0;i

解决方案

int input = Integer.parseInt(JOptionPane.showInputDialog("How many tables would 
                                                         + you like to create?"));

for (i = 0; i < input; i++)
int input=Integer.parseInt(JOptionPane.showInputDialog)(“需要多少个表?”
+你想创作吗?);
对于(i=0;i
当您启动for循环时,您将用户输入的值(存储在
i
中)重置为0,因此for循环将从0开始执行,直到不再满足设置的条件(
i
),因此它将从0变为100。我建议在for循环中使用不同的迭代器,这样就不会覆盖用户输入。此外,您应该重新查看for循环条件(根据实际需要考虑!)。

您要求用户输入值
i
,然后将其设置为0。这样做有什么意义?对于循环踢出,这意味着什么?大概用户只想循环用户想要的表的次数。如果首先将表数组初始化为具有正确的长度,则可以解决此问题。