简体   繁体   中英

Else statement only giving output on odd input

When giving invalid input, the program only returns the "Please enter a valid input." on an odd input, so the first, third, so on.

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.

Here is my Class that is running.

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(); 
}

Is there any way to check the input for each time new input is added?

You can test your logic with a much smaller and simpler program; for example, you could try a version that only accepts cents. This would make the problem more visible. Essentially, your code looks like this:

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

You probably intended to use

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

You can achieve this by using a continue statement when you detect the error:

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

Additionally, as it stands, you present no feedback regarding cashout. Users would expect a press enter to continue, or write "cashout" to exit as part of step 3.


Note that there are several other problems with the code, such as a redundant else, handling plurals, and the fact that it can be rewritten to be both shorter and more readable. You may want to post it on codereview to get expert feedback on how it could be rewritten.

The reason for this anomaly is that on an incorrect input you are reading the input again and assigning it to another variable. After that you are asking the user for another input in the first line of your do-while loop.

To resolve this remove the close variable and just check for exit condition using

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

Thereby, all inputs from user are stored in the variable coinCount . You don't need an extra one for an exit condition.

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