【问题标题】:Do while loop starts on wrong point [Java]执行 while 循环在错误点开始 [Java]
【发布时间】:2018-01-02 09:45:46
【问题描述】:

我对 java 很陌生,对 while 循环也很陌生。 我的循环有问题,我做了一个基本的计算器,并在你计算了一些东西后给它一个选项来计算其他东西。当此选项出现并且我输入“Y”(大写)时,它会“重新启动”,但如果我回答“y”(小写),它会返回您选择的内容。

例如:如果我启动计算器,我会得到 5 个选项,如果我选择第 1 个选项并且我走到最后,我会得到一个选择:如果我想“再试一次”,那么如果我选择回答“Y” 整个事情又回到了“屏幕”,在那里我可以选择做什么。但是当我选择回答“y”时,它又回到了我之前选择的地方。就像我选择选项 1 并回答“y”一样,它会返回选项 1。

我试图在网上查找它,但找不到任何东西,所以要么我不知道使用正确的关键字,要么其他人不会经常遇到我的问题。

这是计算器,里面有几个荷兰语单词,比如计算器或非常糟糕的英语,但它们都不重要:

import java.util.*;

public class GEKENMCHINENENE {
    public static void main(String[] args){

        char aga = 'y';
        while(aga == 'y' || aga == 'Y'){
        Scanner antwoord = new Scanner(System.in);
        int aw;

do{

    System.out.println("enter 1 for addition. enter 2 for subtraction. enter 3 for division. enter 4 for multiplication.");
    System.out.println("+------------------------------+");
    System.out.println("|         Calculator           |");
    System.out.println("|------------------------------|");
    System.out.println("|  Press 1 for addition        |");
    System.out.println("|  Press 2 for subtraction     |");
    System.out.println("|  Press 3 for division        |");
    System.out.println("|  Press 4 for multiplication  |");
    System.out.println("|  Press 5 for my social media |");
    System.out.println("|  *credit to creepershelp*    |");
    System.out.println("|        *on pastebin*         |");
    System.out.println("+------------------------------+");


    aw = antwoord.nextInt();

    do{

        if (aw > 5){
            System.out.println("That number is out of range, try aga within the range of 1 - 4");
        aw = antwoord.nextInt();

        }else if(aw < 1){
            System.out.println("That number is out of range, try aga within the range of 1 - 4");
        aw = antwoord.nextInt();
        }else if(aw < -1){
            System.out.println("That number is out of range, try aga within the range of 1 - 4");
        aw = antwoord.nextInt();
        }

        if(aw == 1){
            System.out.println("You choose for 1, addition. Enter your first number.");
        double fnum = antwoord.nextDouble();
            System.out.println("Enter your second number");
        double snum = antwoord.nextDouble();
        double awp = fnum + snum;
            System.out.println("The answer is " + awp);
            System.out.println("Wood you like to reken uit something else? [Y/N]");
            aga = antwoord.next().charAt(0);


            if(aga == 'n'){
                System.out.println("Shutting down...");
            }if(aga == 'n'){
                aga = 'n';
                antwoord.close();
                break;
            }else if(aga == 'N'){
                System.out.println("Shutting down...");
            }if(aga == 'N'){
                aga = 'N';
                antwoord.close();
                break;
            }else if(aga == 'y'){
                System.out.println("Please wait!");
            }else if(aga == 'Y'){
                System.out.println("Please wait!");
            aw = 0;
            }
        }

        if(aw == 2){
            System.out.println("You choose for 2, subtraction. Enter your first number.");
        double fnum = antwoord.nextDouble();
            System.out.println("Enter your second number");
        double snum = antwoord.nextDouble();
        double awp = fnum - snum;
            System.out.println("The answer is " + awp);
            System.out.println("Wood you like to reken uit something else? [Y/N]");
            aga = antwoord.next().charAt(0);


            if(aga == 'n'){
                System.out.println("Shutting down...");
            }if(aga == 'n'){
                aga = 'n';
                antwoord.close();
                break;
            }else if(aga == 'N'){
                System.out.println("Shutting down...");
            }if(aga == 'N'){
                aga = 'N';
                antwoord.close();
                break;
            }else if(aga == 'y'){
                System.out.println("Please wait!");
            }else if(aga == 'Y'){
                System.out.println("Please wait!");
            aw = 0;
            }
        }

        if(aw == 3){
            System.out.println("You choose for 3, division. Enter your first number.");
        double fnum = antwoord.nextDouble();
            System.out.println("Enter your second number");
        double snum = antwoord.nextDouble();
        double awp = fnum / snum;
            System.out.println("The answer is " + awp);
            System.out.println("Wood you like to reken uit something else? [Y/N]");
            aga = antwoord.next().charAt(0);


            if(aga == 'n'){
                System.out.println("Shutting down...");
            }if(aga == 'n'){
                aga = 'n';
                antwoord.close();
                break;
            }else if(aga == 'N'){
                System.out.println("Shutting down...");
            }if(aga == 'N'){
                aga = 'N';
                antwoord.close();
                break;
            }else if(aga == 'y'){
                System.out.println("Please wait!");
            }else if(aga == 'Y'){
                System.out.println("Please wait!");
            aw = 0;
            }
        }

        if(aw == 4){
            System.out.println("You choose for 4, multiplication. Enter your first number.");
        double fnum = antwoord.nextDouble();
            System.out.println("Enter your second number");
        double snum = antwoord.nextDouble();
        double awp = fnum * snum;
            System.out.println("The answer is " + awp);
            System.out.println("Wood you like to reken uit something else? [Y/N]");
            aga = antwoord.next().charAt(0);


            if(aga == 'n'){
                System.out.println("Shutting down...");
            }if(aga == 'n'){
                aga = 'n';
                antwoord.close();
                break;
            }else if(aga == 'N'){
                System.out.println("Shutting down...");
            }if(aga == 'N'){
                aga = 'N';
                antwoord.close();
                break;
            }else if(aga == 'y'){
                System.out.println("Please wait!");
            }else if(aga == 'Y'){
                System.out.println("Please wait!");
            aw = 0;
            }

        }if(aw == 5){
            System.out.println("Instagram: rubettt, https://www.instagram.com/rubettt/?hl=en");
            System.out.println("Snapchat:  rubet23-1212121");
            System.out.println("Wood you like to reken uit something else? [Y/N]");
            aga = antwoord.next().charAt(0);


            if(aga == 'n'){
                System.out.println("Shutting down...");
            }if(aga == 'n'){
                aga = 'n';
                antwoord.close();
                break;
            }else if(aga == 'N'){
                System.out.println("Shutting down...");
            }if(aga == 'N'){
                aga = 'N';
                antwoord.close();
                break;
            }else if(aga == 'y'){
                System.out.println("Please wait!");
            }else if(aga == 'Y'){
                System.out.println("Please wait!");
            aw = 0;
            }
        }if(aw == 0){
            System.out.println("Succesfully shut down!");
        }else if(aga == 'n' || aga == 'N'){
            System.out.println("Succesfully shut down!");
        }

        }while(aw > 1 && aw <329108321);

        }while(aga == 'y' || aga == 'Y');


        }
    }
}

我试图让它将“y”更改为“Y”,但这也没有用。 如果有人有任何想法,请帮助。我很想知道为什么会这样以及如何解决它。提前致谢,

-Rube23

【问题讨论】:

  • “不工作”是什么意思? StackOverflow 不是调试器的替代品
  • 很难在代码中找到错误,因为它确实与重复案例混淆并且难以阅读。您可能应该重写代码,而不是试图找到错误,并且在这样做之前,您应该对其进行计划。如果您愿意,我可以提供帮助,但除此之外,我无能为力。
  • 创建一个新项目并取出所有代码,除了 do while 循环。在您的 do while 循环结束时会询问问题并选择一个选项。如果它工作正常,开始添加你的其他代码。
  • else if(aw &lt; 1)else if(aw &lt; -1) 之后,你不觉得这里有什么不必要的吗?
  • 欢迎来到 Stack Overflow!在您成为计算机程序员的过程中,我建议您花一些时间来学习如何调试自己的代码。这可以从纸面调试到使用源代码级调试器。这些是任何软件开发人员都需要的关键技能。祝你好运!

标签: java eclipse calculator do-while calc


【解决方案1】:

您在检查小写字母“y”时忘记将 aw 设置为 0,因此 while 循环会重复,因为您从未更改过数字。在最底部,您的代码应该说:

//The stuff above this
else if(aga == 'y'){
    System.out.println("Please wait!");
    aw = 0;
}
//The rest of your code`

这与您的问题无关,但我认为您应该每次都使用 aga.toUpper() 将输入设为大写。这样,您不必为两种情况编写两次相同的代码;你可以只测试字母的大写实例。

祝你好运!

【讨论】:

  • 我相信我的解决方案会奏效;但是,我谦虚地建议您听取上述评论者的建议,花一些时间清理您的代码并删除不必要的行,然后再继续您的项目。它将使您的事情变得更容易,并且您将成为更好的程序员。 :)
【解决方案2】:

注意:这不是答案,只是对代码结构的批评

您输入“是”和“否”的方式很容易出错,因为您一遍又一遍地重用相同的代码。创建一个返回布尔值的方法。例如:

/**
 * This method will return true for yes and false for no.
 * It is private, because it is unnecessary for other
 * Classes to use it.
 */
private boolean sayYes(String text){
    Scanner keyboard = new Scanner(System.in);
    while(true){
        System.out.print(text);
        // Gets keyboard input, trims it, and makes it lower case.
        String input = keyboard.nextLine().trim().toLowerCase;
        // Checks for "Yes"
        if(input.equals("y") || input.equals("yes"))
            return true;
        // Checks for "No"
        if(input.equals("n") || input.equals("no"))
            return false;
        // If we got here, then it was neither a "yes" or "no",
        // so we complain, and go around the loop again
        System.out.println("Invalid input.");
    }
    // The keyboard Scanner is not closed so the program can
    // continue to receive keyboard input from other places.
}

然后你可以调用这个方法:

if(sayYes("Wood you like to reken uit something else? [Y/N]\n")){
    aw = 0;
} else {
    antwoord.close();
    break;
}

这也使输入系统更加健壮,因为他们被给予多次输入以输入响应,并且他们可以在不破坏代码的情况下输入完整的乱码。您也可以制作一个类似的系统来检索号码。

【讨论】:

  • Rubet23 这就是我在回答的评论中所指的那种东西。
【解决方案3】:

您有两个运行while(aga == 'y' || aga == 'Y'); 的循环。两者之间没有区别。如果您希望这两种可能性以两种不同的方式表现,您还需要以两种不同的方式使用它们。

当您将其中一个比较移动到另一个while 条件时,它的行为似乎应该像您希望的那样:

while(aw > 1 && aw <329108321 && aga != 'y');

请注意,您的代码很难阅读,因为您的缩进不一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多