【问题标题】:User validation testing in Java while loopJava while循环中的用户验证测试
【发布时间】:2019-03-02 20:09:26
【问题描述】:

我正在制作我的第一个 java 项目,检查用户输入的单词(没有数字或符号)是否是回文。我使用了此处概述的方法:https://stackoverflow.com/a/4139065/10421526 用于逻辑。该代码按请求工作,但我无法在输入无效后重新提示用户,因为它似乎“阻止”了我的代码接受有效输入的能力,打印了我的“重试”消息。我也尝试过 do while 版本,但最终出现格式错误。我认为我定义我的 stringOut 变量的方式给我带来了麻烦,但我不知道如何改变它而不像 eclipse 所说的那样重复。这是经过数十次尝试后我能得到的最接近的:

import java.util.Scanner;

public class PalindromTester {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);


        System.out.println("Input word to test: ");
        String stringIn = in.nextLine();
        String stringOut = new StringBuilder(stringIn).reverse().toString(); 

        while (!stringIn.matches("[a-zA-Z]+")) {
            System.out.println("Invalid input, try again.");
            in.next(); //stops infinite error loop
        }

        if ((stringIn.equalsIgnoreCase(stringOut))) {
            System.out.println("Palindrome detected");
            System.out.println("You entered: " + stringIn);
            System.out.println("Your string reversed is: " + stringOut);

}       else {
            System.out.println("Not a palindrome");
}

    }
}

【问题讨论】:

  • 问题是你永远不会改变stringIn的值。在您的循环中,您需要执行 stringIn = in.next();

标签: java loops validation


【解决方案1】:

使用do-while 循环的一个很好的用例。当您必须确保您的语句至少执行一次时,您可以使用此循环。并且只有在匹配条件时才会执行后续的执行。在这种情况下,该条件将是验证您的输入

    Scanner in = new Scanner(System.in);

    String prompt = "Input word to test: ";

    String stringIn;

    do {
        System.out.println(prompt);
        stringIn = in.nextLine();
        prompt = "Invalid input, try again.";
    }
    while (stringIn.matches("[a-zA-Z]+"));

如果输入是non-numeric,while 条件将是true,并且会使这个循环再次运行,因此要求新的输入。如果输入为numeric,则while 条件将为false,因此退出while 循环并在stringIn 变量中为您提供用户输入。

【讨论】:

    【解决方案2】:

    将while循环中的in.next();更改为stringIn= in.next();,在while循环之后添加stringOut = new StringBuilder(stringIn).reverse().toString();

    import java.util.Scanner;
    
        public class Test {
    
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                Scanner in = new Scanner(System.in);
    
    
                System.out.println("Input word to test: ");
                String stringIn = in.nextLine();
                String stringOut = new StringBuilder(stringIn).reverse().toString(); 
    
                while (!stringIn.matches("[a-zA-Z]+")) {
                    System.out.println("Invalid input, try again.");
                    stringIn= in.next(); //stops infinite error loop
                }
                stringOut = new StringBuilder(stringIn).reverse().toString();
                if ((stringIn.equalsIgnoreCase(stringOut))) {
                    System.out.println("Palindrome detected");
                    System.out.println("You entered: " + stringIn);
                    System.out.println("Your string reversed is: " + stringOut);
    
        }       else {
                    System.out.println("Not a palindrome");
        }
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 2014-12-08
      • 2021-10-14
      • 2013-10-08
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 2020-06-20
      相关资源
      最近更新 更多