简体   繁体   中英

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.

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".

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:

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.

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 ; in your case i statement ,this will facilitate adding of value in your saldo variable.

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;

            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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