java - 请帮忙,在 while 循环中询问某些问题时遇到问题

标签 java loops while-loop

我试图要求用户输入是或否,如果是,那么它将重新启动,并且将添加特征,因为他们做了 2 个选择。然而,它不允许我问是/否,而且我显然在某个地方搞砸了。如果可能的话,我如何才能最大限度地减少代码,而不是使用那个令人眼花缭乱的优化不佳的 if 语句。

public static void main(String[]args)
    {
        //Number 1
        Scanner scan = new Scanner(System.in);
        int num = 0;
        int num2 = 0;
        int num3 = 0;
        int count = 2;
        String yesno;
        do
        {
            System.out.println("Which aircraft would you like to simulate?");
            System.out.println("1. Blimp");
            System.out.println("2. Helicopter");
            System.out.println("3. Fighter Jet");
            System.out.println("4. Space Shuttle");
            num = scan.nextInt();
            if(num<1 || num>4) {System.out.println("Invalid");}

        } 
        while(num<1 || num>4);
        do
        {

            System.out.println("What characteristics would you like? (Input one, then the other)");
            System.out.println("1. Position Trim ");
            System.out.println("2. Force Breakout");
            System.out.println("3. Force Gradient");
            System.out.println("4. Force Friction");
            System.out.println("5. Damping");
            System.out.println("6. Hard Stop");
            num2 = scan.nextInt();
            num3 = scan.nextInt();

            if(num2 == 1 || num3 == 1)
            {
                System.out.println("The position to which a flight control returns");
            }
            if(num2 == 2 ||num3 == 2)
            {
                System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
            }
            if(num2 == 3 || num3 == 3)
            {
                System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
            }
            if (num2 == 4 || num3 == 4)
            {
                System.out.println("A constant force that is opposite to the direction of movement");
            }
            if(num2 == 5 || num3 == 5)
            {
                System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
            }
            if (num2 == 6 || num3 == 6)
            {
                System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
            }
            if(num2 < 1 || num2 > 6)
            {
                System.out.println("Invalid input");

            }

            if (yesno.equalsIgnoreCase("y")) {
                do
                {

                    System.out.println("What characteristics would you like? (Input one, then the other)");
                    System.out.println("1. Position Trim ");
                    System.out.println("2. Force Breakout");
                    System.out.println("3. Force Gradient");
                    System.out.println("4. Force Friction");
                    System.out.println("5. Damping");
                    System.out.println("6. Hard Stop");
                    num2 = scan.nextInt();
                    num3 = scan.nextInt();

                    if(num2 == 1 || num3 == 1)
                    {
                        System.out.println("The position to which a flight control returns");
                    }
                    if(num2 == 2 ||num3 == 2)
                    {
                        System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
                    }
                    if(num2 == 3 || num3 == 3)
                    {
                        System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
                    }
                    if (num2 == 4 || num3 == 4)
                    {
                        System.out.println("A constant force that is opposite to the direction of movement");
                    }
                    if(num2 == 5 || num3 == 5)
                    {
                        System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
                    }
                    if (num2 == 6 || num3 == 6)
                    {
                        System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
                    }
                    if(num2 < 1 || num2 > 6)
                    {
                        System.out.println("Invalid input");

                    }
                    System.out.println("Would you like to re-select?(Y/N)");
                    yesno = scan.nextLine();
                    count += 2;
                }
                while(num2 < 1 || num2 > 6 || num3 < 1 ||num3 > 6 );

            }
            else if (yesno.equalsIgnoreCase("n"))
            {
                System.out.println("You've used " + count + " characteristics");
            }

        }
        while(num2 < 1 || num2 > 6 || num3 < 1 ||num3 > 6 );

    }
}

最佳答案

您的代码可以压缩一些,但主要问题是 yesno 变量从未初始化,因此条件 if (yesno.equalsIgnoreCase("y")) 计算结果为 false 并且后续 block 从不计算,因此这些行

    System.out.println("Would you like to re-select?(Y/N)");
    yesno = scan.nextLine();

永远不会被评估,也永远不会询问用户。如果您保留代码不变,则应在创建代码时设置 yesno="y"。但是,就像我说的,您的代码可以简化。

关于java - 请帮忙,在 while 循环中询问某些问题时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677526/

相关文章:

java - java 8 设置中捆绑的 JavaDB

javascript - 通过 jQuery/CSS 在 for 循环内右对齐文本字段

c - 打印空格数

变量的javascript递增名称

python - 可以使这段代码更 Pythonic 吗?循环

java.lang.reflect.InitationTargetException,由: java. lang.NoClassDefFoundError引起:com/fasterxml/jackson/databind/ObjectMapper

java - 响应代码 422 转到 onFailure 方法

java - 我如何通过我在 GWT 中的历史记录来确定我是进入 "forward"还是 "backward"?

java - 我正在从文件中读取,if 循环或 Why 循环没有选取第一个按钮 "7",但获取所有其余按钮

java - While 循环不起作用