简体   繁体   English

需要帮助来创建简单的“银行”菜单

[英]Need help creating simple “bank” menu

I am very new at programming and trying to learn as much as I can. 我对编程非常陌生,并尝试学习尽可能多的东西。 I have had a simple problem with my code the last few days, and I don't really now how I should do! 最近几天,我的代码遇到了一个简单的问题,现在我真的不知道该怎么做! I am trying to make a "bank" menu. 我正在尝试制作“银行”菜单。

In which you will be able to insert money[i], [u]withdraw money, [s]check the balance or [a]close the program. 您可以在其中插入资金[i],[u]提取资金,[s]检查余额或[a]关闭程序。

My question is, when the users chooses to insert money, I want to scan in their answer, make it an int and then add to the int i use to "check the balance". 我的问题是,当用户选择插入货币时,我想扫描他们的答案,将其设置为整数,然后添加到i用来“检查余额”的整数中。

So if I first choose to insert 300, when I then come back to the menu and choose to check balance [s] I want those 300 to show there!Here is my current code: 因此,如果我首先选择插入300,然后回到菜单并选择查看余额[s],则我希望那300显示在此处!这是我当前的代码:

Scanner scan = new Scanner(System.in);
while (true) {
    System.out.println("[I]nsättning");
    System.out.println("[U]ttag");
    System.out.println("[S]aldo");
    System.out.println("[A]vsluta");
    String menu = scan.next();
    switch (menu) {
        case "i":
            System.out.print("Hur mycket pengar vill du sätta in på ditt konto? : ");
            String str = scan.next();
            int insättning = Integer.parseInt(str);
            saldo = saldo + insättning;

        case "s":
            System.out.println("Du har : " + saldo + "kr på ditt konto");
    }
}

If I understand your problem right then, your problem is whenever you press 'i' you are correctly entering 'case i' statement ,but that amount is not getting added to your total value ie saldo variable. 如果我现在就了解您的问题,那么您的问题就是每当您按'i'时,您就正确地输入了'case i'语句,但是该金额未添加到您的总价值(即saldo变量)中。

you need to do something like this 你需要做这样的事情

    case ("i"):
          System.out.print("Hur mycket pengar vill du sätta in på ditt konto? : ");//Translate" How much money do you want to put in u  account?"
          String str = scan.next();
          int insättning = Integer.parseInt(str); 
          saldo = saldo + insättning ;

you are correctly retrieving the value, but not using it in your code , so you need to add saldo = saldo + insättning ; 您正确地获取了该值,但是没有在代码中使用它,因此您需要添加saldo = saldo + insättning ; in your case i statement ,this will facilitate adding of value in your saldo variable. 在您的情况下,我声明,这将有助于在saldo变量中增加价值。

I dont know what's the problem with your code , but below code surely works for me 我不知道您的代码有什么问题,但是下面的代码肯定对我有用

public static void main(String[] args) {
        int saldo = 0;
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("[I]nsättning");
            System.out.println("[U]ttag");
            System.out.println("[S]aldo");
            System.out.println("[A]vsluta");
            String menu = scan.next();
            switch (menu) {
            case ("i"):
                System.out.print("Hur mycket pengar vill du sätta in på ditt konto? : ");
                String str = scan.next();
                int insättning = Integer.parseInt(str);
                saldo = saldo + insättning;
                break;
            case ("s"):
                System.out.println("Du har : " + saldo + "kr på ditt konto");
                break;

            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM