输入字符串检查

Rya*_*les 0 java

我正在编写一个ATM程序,当用户输入其中一个字符串值时,程序应检查它并相应地执行一个方法.问题代码在这里:

System.out.println("PRESS");
            System.out.println("(D)eposit");
            System.out.println("(W)ithdraw");
            System.out.println("(C)heck Account Balance");
            System.out.println("(Q)uit");
            System.out.println("Enter Choice: ");
            String choice = scanner.nextLine();
            scanner.nextLine();
            if(choice == "D"){
                currentCustomer.deposit();
            }
            else if(choice == "W"){
                currentCustomer.withdraw();
            }
            else if(choice == "C"){
                currentCustomer.checkBalance();
            }
            else if(choice == "Q"){
                currentCustomer.quit();
            }
            else{
                System.out.println("Invalid choice please reenter: ");
            }
Run Code Online (Sandbox Code Playgroud)

如果用户输入"D",程序将跳至else语句.我知道在使用时.nextLine你必须使用两个因为返回字符,但我不确定这种情况是否属实.无论哪种方式,如果我有额外的.nextLine声明,它仍然会向前跳过.任何帮助将非常感激!

Mar*_*oun 6

在Java中,我们将字符串与String#equals进行比较.

我不会在equals==谷歌之间写下区别以获取更多信息.你将获得大约100个结果.