簡體   English   中英

用Java檢查字符串數組中項目的輸入

[英]Checking Input Against Items in a String Array in Java

我有以下代碼,我正在為我的大學任務工作。 我被要求使用我正在使用的數組。 我被要求使用for循環和if語句,我已經在做了。 我想出了以下代碼:

class HardwareStore2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.printf("%55s", "**WELCOME TO THE HARDWARE STORE**\n");
        System.out.printf("%55s", "=================================\n");

        String [] codes = {"G22", "K13", "S21", "I30"};
        String [] description = {"STICKY Construction Glue", "CAR-LO Key Ring", "SCREW-DUP Screwy Screws", "LET-IT-RAIN Padlock"};
        List<String> codeList = Arrays.asList(codes);
        String output = "";
        int i = 1000;
        String [] userCode = new String[i];
        char dolSymb = '$';
        int [] pricesAndTax = {10989, 5655, 1099, 4005, 20};
        int [] weight = {};
        int [] userQuantity = {1};
        int [] userPrice = new int[i];
        int userStickyPrice = 0;
        int userKeyringPrice = 0;
        int userScrewyPrice = 0;
        int userPadlockPrice = 0;
        int userPreWithTax = 0;
        int userTotal = 0;
        int userPreTotal = 0;
        int userShipping = 0;

        System.out.printf("%-10s%-40s%-15s%-10s\n", "CODE", "DESCRIPTION", "WEIGHT", "PRICE\n");
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[0], description[0], dolSymb, pricesAndTax[0]/100, pricesAndTax[0]%100);
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[1], description[1], dolSymb, pricesAndTax[1]/100, pricesAndTax[1]%100);
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[2], description[2], dolSymb, pricesAndTax[2]/100, pricesAndTax[2]%100);
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[3], description[3], dolSymb, pricesAndTax[3]/100, pricesAndTax[3]%100);

        System.out.println("PLEASE ENTER YOUR ORDER:");
        System.out.print("NAME: ");
        String username = in.nextLine();
        System.out.print("ADDRESS Line 1: ");
        String address1 = in.nextLine();
        System.out.print("ADDRESS Line 2: ");
        String address2 = in.nextLine();
        System.out.print("POSTAL CODE: ");
        String postalcode = in.nextLine();

        for (i = 0;; i++) {
            System.out.print("CODE (X to QUIT):");
            userCode[i] = in.nextLine();

            if (userCode[i].equalsIgnoreCase("x")) {
                break;
            }

            System.out.print("QUANTITY: ");
            userQuantity[i] = in.nextInt();
            in.nextLine();

            if (userCode[i].equalsIgnoreCase(codes[0])) {
                userStickyPrice += userQuantity[i]*pricesAndTax[0];

            }
            else if (userCode[i].equalsIgnoreCase(codes[1])) {
                userKeyringPrice += userQuantity[i]*pricesAndTax[1];

            }
            else if (userCode[i].equalsIgnoreCase(codes[2])) {
                userScrewyPrice += userQuantity[i]*pricesAndTax[2];

            }
            else if (userCode[i].equalsIgnoreCase(codes[3])) {
                userPadlockPrice += userQuantity[i]*pricesAndTax[3];

            }
            else if (!codeList.contains(userCode)) {
                i = i - 1;
            }
        }
     }
}

現在,一切都與一百萬個ifs和elses無縫協作,但我想知道是否有辦法替換所有if-else-if語句中的一個或兩個執行類似這樣的操作:

if (userCode[i].contains(codes[0], codes[1], codes[2], codes[3] {}

或者更像是:

if (userCode.contains(any.one.item.in.codeList) {
    then.get.the.price.of.that.item.and.do.item.specific.operations
    }

如果問題不夠清楚,請告訴我。 再一次,這是一項大學任務,所以我很感激解釋。

在不更改其余數據結構以獲得更高效的內容(例如, Map )的情況下,您可以使用單個if和嵌套循環實現相同的效果:

boolean found = false;
for (int j = 0 ; !found && j != codes.length ; j++) {
    if (userCode[i].equalsIgnoreCase(codes[j])) {
        userScrewyPrice += userQuantity[i]*pricesAndTax[j];
        found = true;
    }
}
if (!found) {
    i--;
}

變量codeList是一個List ,它有一個contains函數,如果我理解你要做的事情可能會有所幫助。

此外,從Java 7您可以使用字符串作為switch語句的參數,這將使您的代碼看起來更好。

我沒有閱讀完整的問題所以這可能只是一個部分答案...但你問你是否可以使用:

if (userCode[i].contains(codes[0], codes[1], codes[2], codes[3] {}

你也許可以使用......

if(new string[]{"a","b","c"}.Contains("a")) {}

或者把它放到自定義數組類型中......

arrayType[] a = new arrayType[]{item1, item2, item3}
if (arrayType.Contains(searchItem)) {}

基本上 - 你可以做你所要求的,只需要改變語法的順序。 但是,我確信,這只是讓你思考的部分答案。

任何時候你有多個“if”語句,你也應該看一下使用“switch”語句。

看起來像switch語句會在這里保存很多行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM