while循环是否在错误的点上启动[Java]

时间:2017-07-26 21:20:17

标签: java eclipse calculator do-while calc

我对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');


        }
    }
}

我试图改变它&#34; y&#34;进入&#34; Y&#34;但这也行不通。 如果有人有任何想法,请帮忙。我很想知道为什么会这样,以及如何解决它。提前谢谢,

-Rubet23

3 个答案:

答案 0 :(得分:0)

当您检查小写字母&#39; y时,您忘了将aw设置为0。所以while循环重复,因为你从未改变过这个数字。在最底部,您的代码应该说:

apostrophe-workflow

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

祝你好运!

答案 1 :(得分:0)

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

当你将其中一个比较移动到另一个while条件时,它似乎应该像你想要的那样:

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

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

答案 2 :(得分:0)

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

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

//We query local context first to see if it's there.
var attachedEntity = db.Carts.Local.Find(newCart.Id);

//We have it in the context, need to update.
if (attachedEntity != null) {
    var attachedEntry = _context.Entry(attachedEntity);
    attachedEntry.CurrentValues.SetValues(newCart);
}

然后您可以使用以下方法调用此方法:

/**
 * 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.
}

这也允许输入系统更加健壮,因为它们被赋予多个镜头以输入响应,并且他们可以在不破坏代码的情况下键入完整的乱码。您还可以创建一个类似的系统来检索数字。