阵列被无意间连续填充在开关柜内部

问题描述

我目前正在处理最近在做的事情。为了简单明了,我希望我的程序按以下方式运行:

  1. 用户输入一个数字,然后检查该数字以查看其是否有效。如果该选项无效,则会显示一条错误消息,并使主菜单再次出现。

  2. 如果选择了有效选项,则切换用例将采用选定的选项并运行每个case语句内部的代码。

  3. 一旦代码在切换用例部分中完成运行,它将循环回到顶部并重新显示主菜单,并且过程1-3将继续运行直到用户选择选项4退出程序。

我似乎遇到的问题是,当进入情况1时,它喜欢循环遍历本不打算循环的某些代码,并继续向数组中添加Triangle对象,这是这件作品:

if((side1 == 0 || side2 == 0 || side3 == 0)) 
{
        // Problem with do-while loop begins here
        triangleArray[trianglesAdded] = new Triangle();
        System.out.println("Default triangle created");
        trianglesAdded += 1;
}

其中,triangleArray是大小为100的Triangle对象的数组,triangle Leicester会跟踪实际向该数组中添加了多少个三角形,并且是该数组索引的变量。由于它一直向该数组添加Triangle对象,因此出现错误,因为一旦达到其极限,该数组将不再容纳Triangle对象,这意味着我也无法循环回到主菜单。

所以我的问题是:我如何才能使其停止在该部分中循环(因为它只应该发生一次,并且一次只向数组添加一个Triangle对象)并使其遍历整个圈并在运行后再次显示主菜单?

完整代码如下:

do 
    {
        while(validMenuOption == false) 
        {
            System.out.println("1. Enter data for a new triangle.");
            System.out.println("2. Print all triangles sorted by area,smallest to largest.");
            System.out.println("3. Print only triangles with a specific color.");
            System.out.println("4. Exit the program.");
            System.out.print("\nPlease enter in a number to select an option: ");
            menuOption = sc.nextInt();
            
            if(menuOption >= 1 && menuOption <= 4) 
            {
                validMenuOption = true;
            }
            
            else 
            {
                System.out.println("Invalid menu choice. Please try again.\n");
                validMenuOption = false;
            }
            
            if(menuOption == 1 && trianglesAdded == triangleArray.length - 1)
            {
                System.out.println("The triangle array has hit the max. You are unable to add more triangles.");
                validMenuOption = false;
            }
        }
        
        switch(menuOption) 
        {
            case 1:
                ... 
                
                // If one,two,or all of the side lengths given are zero and the color of the triangle is 1,creates a default triangle
                // with the sides being 1 and the color being 1
                if((side1 == 0 || side2 == 0 || side3 == 0)) 
                {
                    // Problem begins here
                    triangleArray[trianglesAdded] = new Triangle();
                    System.out.println("Default triangle created");
                    trianglesAdded += 1;
                }
                
                // If side lengths greater than zero were given,creates a triangle with the side lengths and color given
                else 
                {
                    triangleArray[trianglesAdded] = new Triangle(side1,side2,side3,color);
                    System.out.println("Triangle created with specified size and color");
                }
                
                break;
                
            case 2:
                ...
                break;
                
            case 3:
                ...
                break;
                
            case 4:
                ...
                break;
                
            default:
                ...
                break;
        }
    } while(true);

解决方法

嗯,您有一个错误。因此,当遇到错误时,您就可以做每个人要做的事情:您可以逐行计算程序在脑海中应该做的事情。然后,将其与计算机告诉您的代码含义相提并论。在大脑和计算机意见分歧的地方,您发现了一个错误。

  1. 您的程序说'validMenuOption'为假。
  2. 我输入“ 1”。
  3. validMenuOption设置为true,我们循环输入“ while输入”。
  4. vMO现在是正确的,因此while循环结束了,我们继续。
  5. 开关运行,我们执行“添加三角形”部分。
  6. 外循环循环。
  7. validMenuOption仍然为真
  8. 所以..整个循环“运行零次”就“得到一些输入”。

大概是您想先重置有效菜单选项,还是希望它是d o {} while ();循环。

,

我将完全删除validMenuOption变量并将其移至单独的函数,如下所示:

public int getMenuOption() {
        Scanner sc = new Scanner(System.in);
        int menuOption = 0;

        System.out.println("1. Enter data for a new triangle.");
        System.out.println("2. Print all triangles sorted by area,smallest to largest.");
        System.out.println("3. Print only triangles with a specific color.");
        System.out.println("4. Exit the program.");
        System.out.print("\nPlease enter in a number to select an option: ");
        
        menuOption = sc.nextInt();

        if(menuOption < 1 || menuOption > 4) {
            return 0;
        } else {
            return menuOption;
        }
    }

查看选项超出范围时如何返回0,否则返回menuOption

现在在您的main或任何运行switch的语句中执行以下操作:

        boolean run = true;
        int[] triangleArray = new int[100];
        int trianglesAdded = 0;

        while (run) {
            menuOption = getMenuOption();
            switch(menuOption) {
                case 0:
                    System.out.println("Invalid menu choice. Please try again.\n");
                    break;
                case 1:
                    if(trianglesAdded == triangleArray.length - 1) {
                        System.out.println("The triangle array has hit the max. You are unable to add more triangles.");
                        run = false;
                    } else {
                        triangleArray[trianglesAdded] = 1; // or whatevery number or object you want
                        System.out.println("Default triangle created");
                        trianglesAdded += 1;
                    }
                    break;
                default:
                    break;
            }
        }

为简单起见,我将triangleArray设为int数组。 我还添加了一个boolean变量run,该变量在将其设置为true时运行,并在输入while时从menuOption函数获取getMenuOption()

现在,如果它得到0,则会打印菜单选项为假break,然后返回循环的开头,然后再次返回getMenuOption()。如果输入1,则输入case 1

您提到的问题:

我收到一个错误,因为一旦达到极限,数组将无法再容纳任何Triangle对象

可以通过添加condition来解决问题,该方法可以像我一样在添加之前检查长度

要退出它,您需要添加另一个case,它将run设置为false

case 4:
     run = false;

这里是repl,供您立即进行测试。我已将数组的大小更改为2,以便在达到限制时可以看到数组停止。

,

您可以尝试以下代码

while(true){

            System.out.println("1. Enter data for a new triangle.");
            System.out.println("2. Print all triangles sorted by area,smallest to largest.");
            System.out.println("3. Print only triangles with a specific color.");
            System.out.println("4. Exit the program.");
            System.out.print("\nPlease enter in a number to select an option: ");
            menuOption = sc.nextInt();

                switch(menuOption){
                case 1:
                    if(trianglesAdded == triangleArray.length - 1)
                        {
                            System.out.println("The triangle array has hit the max. You are unable to add more triangles.");
                            return;
                        } 
                                    
                    // If one,two,or all of the side lengths given are zero and the color of the triangle is 1,creates a default triangle
                    // with the sides being 1 and the color being 1
                    if((side1 == 0 || side2 == 0 || side3 == 0)) 
                    {
                        // Problem begins here
                        triangleArray[trianglesAdded] = new Triangle();
                        System.out.println("Default triangle created");
                        trianglesAdded += 1;
                    }
                    
                    // If side lengths greater than zero were given,creates a triangle with the side lengths and color given
                    else 
                    {
                        triangleArray[trianglesAdded] = new Triangle(side1,side2,side3,color);
                        System.out.println("Triangle created with specified size and color");
                    }
                    
                    break;
                    
                case 2:
                    ...
                    break;
                    
                case 3:
                    ...
                    break;
                    
                case 4:
                    ...
                    break;
                    
                default:
                    System.out.println("Invalid menu choice. Please try again.\n");
                    break;
            }

        }
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...