【问题标题】:Java If Else Statement doesn't trigger for substringsJava If Else 语句不会为子字符串触发
【发布时间】:2021-08-27 19:56:24
【问题描述】:

所以我正在为一个学校项目开发这个程序,我必须创建一个 ATM。出于某种原因,在下面的程序中输入 W 不会做任何事情,但 R 按预期工作。我尝试使用 Else If 并将“R”代码放在“W”代码之上,但似乎没有任何效果。抱歉,代码太长了,谢谢。

import java.util.ArrayList;

public class ConsutingProblem {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int totalCash;
        String choice;
        String rrr = "R";
        String www = "W";
        String iii = "I";
        String qqq = "Q";
        boolean stopAtm = false;
        int hundred = 10;
        int fifty = 10;
        int twenty = 10;
        int ten = 10;
        int five = 10;
        int one = 10;
        totalCash = (hundred*100) + (fifty*50) + (twenty*20) + (ten*10) + (five*5) + one;
        do {
            System.out.println("Please enter the letter corresponding to your chosen action.");
            System.out.println("R for a restock.");
            System.out.println("W for withdrawl followed by amount. Example: W $145");
            System.out.println("I for number of bills present with that denomination. Example: I $20");
            System.out.println("Q for quitting this aplication.");
            choice = input.nextLine();
            if ((choice.substring(0, 0)).equals(www)) {
                int withdrawlAmount = Integer.parseInt(choice.substring(3));
                totalCash -= withdrawlAmount;
                int displayWithdrawl = withdrawlAmount;
                int hundredBack = (int)(displayWithdrawl/100);
                displayWithdrawl -= (hundredBack*100);
                int fiftyBack = (int)(displayWithdrawl/50);
                displayWithdrawl -= (fiftyBack*50);
                int twentyBack = (int)(displayWithdrawl/20);
                displayWithdrawl -= (twentyBack*20);             
                int tenBack = (int)(displayWithdrawl/10);
                displayWithdrawl -= (tenBack*10);
                int fiveBack = (int)(displayWithdrawl/5);
                displayWithdrawl -= (fiveBack*5);
                int oneBack = (int)(displayWithdrawl/1);                   
                displayWithdrawl -= (oneBack*1);
                if (withdrawlAmount <= totalCash && hundredBack <= hundred && fiftyBack <= fifty && twentyBack <= twenty && tenBack <= ten && fiveBack <= five && oneBack <= one) {
                    System.out.println("Success: Dispensed $" + withdrawlAmount);
                    hundred -= hundredBack;
                    fifty -= fiftyBack;
                    twenty -= twentyBack;
                    ten -= tenBack;
                    five -= fiveBack;
                    one -= oneBack;
                    System.out.println("Machine Balance:");
                    System.out.println("$100 - " + hundred);
                    System.out.println("$50 - " + fifty);
                    System.out.println("$20 - " + twenty);
                    System.out.println("$10 - " + ten);
                    System.out.println("$5 - " + five);
                    System.out.println("$1 - " + one);
                } else {
                    System.out.println("Faliure: insufficient funds");
                }
            }
            if (choice.equals(rrr)) {
                hundred = 10;
                fifty = 10;
                twenty = 10;
                ten = 10;
                five = 10;
                one = 10;
                totalCash = (hundred*100) + (fifty*50) + (twenty*20) + (ten*10) + (five*5) + one;
                System.out.println("Machine Balance:");
                System.out.println("$100 - " + hundred);
                System.out.println("$50 - " + fifty);
                System.out.println("$20 - " + twenty);
                System.out.println("$10 - " + ten);
                System.out.println("$5 - " + five);
                System.out.println("$1 - " + one);
            }
        } while (stopAtm == false);


    }
}

【问题讨论】:

    标签: java string if-statement


    【解决方案1】:

    substring(0, 0) 不返回字符串,因为 substring 的第二个索引是独占的。它应该是 substring(0, 1),以获取字符串的第一个字符

    【讨论】:

    • 一些吹毛求疵:substring(0,0) 返回一个字符串(尽管是一个空字符串)
    【解决方案2】:

    这里看起来很奇怪:

    choice.substring(0, 0)).equals(www)
    

    来自文档: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)

    "hamburger".substring(4, 8) 返回 "urge"

    【讨论】:

      【解决方案3】:

      第一个 if 语句中的子字符串是问题所在。或许尝试做类似于后面的 if 语句(rrr)的语句。

      顺便说一句,您还可以通过 String.charAt 方法检查一个特定字符:Is it possible to get only the first character of a String?

      【讨论】:

      • 不能和 rrr 一样,因为W 后面跟着一个数字,R 是一个无参数命令。不过可以使用choice.startsWith("W ")
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2012-07-06
      • 1970-01-01
      相关资源
      最近更新 更多