繁体   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