簡體   English   中英

其他語句僅在奇數輸入上給出輸出

[英]Else statement only giving output on odd input

當給出無效輸入時,程序僅返回“請輸入有效輸入”。 在奇數輸入上,因此第一,第三等。

Welcome to ConversionKiosk by: xxx xxx.
Please deposit any number of coins and follow the number by the name of the coin
Valid coin names are quarters, dimes, nickels, and pennies.
Please enter 'Cashout' to complete the transaction.
poo
Please enter a valid input.
poo
poo
Please enter a valid input.

這是我正在上課的班級。

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

    CoinConverter kiosk = new CoinConverter();

    System.out.println("Welcome to ConversionKiosk by: xxx xxx.\nPlease deposit any number of coins and follow the number by the name of the coin.\nValid coin names are quarters, dimes, nickels, and pennies.\nPlease enter 'Cashout' to complete the transaction.");
    int x = 0;
    do{
        String coinCount = in.nextLine();
        if(coinCount.contains("quarters")) {
            coinCount = coinCount.replace(" quarters", "");

            kiosk.addQuarters(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("dimes")) {
            coinCount = coinCount.replace(" dimes", "");

            kiosk.addDimes(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("nickels")) {
            coinCount = coinCount.replace(" nickels", "");

            kiosk.addNickels(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");            
        }else if(coinCount.contains("pennies")) {
            coinCount = coinCount.replace(" pennies", "");

            kiosk.addPennies(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("quarter")) {
            coinCount = coinCount.replace(" quarter", "");

            kiosk.addQuarters(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("dime")) {
            coinCount = coinCount.replace(" dime", "");

            kiosk.addDimes(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("nickel")) {
            coinCount = coinCount.replace(" nickel", "");

            kiosk.addNickels(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");                
        }else if(coinCount.contains("penny")) {
            coinCount = coinCount.replace(" penny", "");

            kiosk.addPennies(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(!coinCount.contains("quarter") || !coinCount.contains("quarters") || !coinCount.contains("dime") || !coinCount.contains("dimes") || !coinCount.contains("nickel") || !coinCount.contains("nickels") || !coinCount.contains("penny") || !coinCount.contains("pennies")) {
            System.out.println("Please enter a valid input.");
        }else {
            System.out.println("Please enter a valid input.");
        }
        String close = in.nextLine();
        if (close.contains("Cashout")) {
            System.out.println("Would you like another Transaction(y/n)?");
            String another = in.nextLine();
            if(another.contains("y")) {
                kiosk.getVoucher();
            }else {
                kiosk.getVoucher();
                x ++;
            }
        }
    }while (x == 0);
    in.close(); 
}

每次添加新輸入時,是否有任何方法可以檢查輸入?

您可以使用更小,更簡單的程序來測試邏輯。 例如,您可以嘗試僅接受美分的版本。 這將使問題更加明顯。 本質上,您的代碼如下所示:

while not bored,
  1. ask for coins
  2. process each coin type
     or if nothing processed, complain
  3. check for cashout

您可能打算使用

while not bored,
  1. ask for coins
  2. process each coin type
     or if nothing processed, complain and go back to step 1 <--- !
  3. check for cashout

您可以通過在檢測到錯誤時使用continue語句來實現此目的:

    else {
        System.out.println("Please enter a valid input.");
        continue; // <-- repeats loop from beginning, skipping remaining code
    }

此外,就目前而言,您沒有提供有關兌現的反饋。 用戶希望press enter to continue, or write "cashout" to exit步驟3。


請注意,代碼還存在其他一些問題,例如多余的其他代碼,處理復數形式以及可以將其重寫為更短和更易讀的事實。 您可能希望將其發布在codereview上,以獲得有關如何重寫它的專家反饋。

出現此異常的原因是,在輸入錯誤的情況下,您將再次讀取輸入並將其分配給另一個變量。 之后,您要在do-while循環的第一行中向用戶詢問其他輸入。

要解決此問題,請刪除close變量,然后使用來檢查退出條件

if (coinCount.contains("Cashout")) {
  // rest of the code
}

因此,來自用戶的所有輸入都存儲在變量coinCount 您不需要額外的退出條件。

暫無
暫無

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

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