【问题标题】:How to validate the selection menu using a loop如何使用循环验证选择菜单
【发布时间】:2016-08-23 03:02:56
【问题描述】:

有人可以编辑我的代码以使其循环选择菜单吗?如果选择不是 5 个选项之一,它将提示用户重新输入,直到它是一个有效的选项。如果可能的话,解释也会有所帮助。谢谢

这是我的代码。

import java.util.*;
public class ShapeLoopValidation
{
    public static void main (String [] args)
    {
        chooseShape();
    }

    public static void chooseShape()
    {
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("Select a shape number to calculate area of that shape!");
            System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
            int shapeChoice = sc.nextInt();
            //while (true) {
            if (shapeChoice >= 1 && shapeChoice <=4)
            {
                if (shapeChoice == 1)
                {
                    circle();
                }
                else if (shapeChoice == 2)
                {
                    rectangle();
                }
                else if (shapeChoice == 3)
                {
                    triangle();
                }
                else if (shapeChoice == 4)
                {
                    return;
                }
            }
            else
            {
                System.out.print("Error : Choice " + shapeChoice + "Does not exist.");
            }
        }

        class Test {
            int a, b;

            Test(int a, int b) {
                this.a = a;
                this.b = b;
            }
        }
    }

【问题讨论】:

  • 正确的代码缩进
  • "有人可以编辑我的代码以使其循环选择菜单。如果选择不是 5 个选项之一,它将提示用户重新输入,直到它是一个有效的选项。"

标签: java validation loops


【解决方案1】:

首先:看看switch

第二:阅读一下do-while loops(它们通常很适合这种情况)。

现在,我将如何实现它(但您应该真正学习如何在这种情况下创建循环):

public static void chooseShape () {

    boolean valid = false;
    do {
        Scanner sc = new Scanner(System.in);
        System.out.println("Select a shape number to calculate area of that shape!");
        System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
        int shapeChoice = sc.nextInt();

        switch (shapeChoice) {
            valid = true;
            case 1:
                circle();
                break;
            case 2:
                rectangle();
                break;
            case 3:
                triangle();
                break;
            case 4:
                return;
            default:
                valid = false;
                System.out.println("Error : Choice " + shapeChoice + "Does not exist.");
                System.out.println("Please select one that exists.") 
        }
    } while (!valid)
}

【讨论】:

    【解决方案2】:

    在输入退出代码之前使用do-while 流控制:

    int shapeChoice;
    do {
       System.out.println("Select a shape number to calculate area of that shape!");
       System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
       int shapeChoice = sc.nextInt();
       // then use if-else or switch   
    } while (shapeChoice != 4);
    

    使用break 语句在您的代码处循环中断,如下所示:

    else if (shapeChoice == 4)
    {
       break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-15
      • 2016-07-30
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多