简体   繁体   English

Java While循环中的用户验证测试

[英]User validation testing in Java while loop

I am making one of my first java projects checking if a user input word (no numbers or symbols) is a palindrome. 我正在做我的第一个Java项目之一,检查用户输入的单词(没有数字或符号)是否是回文。 I used the method outlined here: https://stackoverflow.com/a/4139065/10421526 for the logic. 我使用了此处概述的方法: https : //stackoverflow.com/a/4139065/10421526作为逻辑。 The code works per-request but I am having trouble re-prompting the user after invalid input as it seems to "block" the ability of my code to accept valid input, printing my "retry" message. 该代码按请求运行,但是在输入无效后我无法重新提示用户,因为它似乎“阻止”了代码接受有效输入的能力,并打印了“重试”消息。 I have also tried the do while version but I ended up with formatting errors. 我也尝试过do while版本,但最终出现格式错误。 I think the way I define my stringOut variable is giving me trouble but Im not sure how to change that without making a duplicate as eclipse says. 我认为定义stringOut变量的方式给我带来了麻烦,但是我不确定如何在不做如日食所说的重复的情况下进行更改。 Here is the closest I could get after dozens of tries going in circles: 这是经过数十次尝试后可以得到的最接近的结果:

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

    }
}

A very good use-case to use do-while loop. 使用do-while循环的一个很好的用例。 You use this loop when you have to make sure that your statements are executed at least once. 当您必须确保语句至少执行一次时,可以使用此循环。 And the subsequent execution is executed only if it matches a condition. 并且只有在满足条件时才执行后续执行。 In this case, that condition would be validating your input . 在这种情况下,该条件将验证您的input

    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]+"));

If the input is non-numeric the while condition would be true and will make this loop run again, hence asking for new input. 如果输入non-numeric ,则while条件为true ,这将使此循环再次运行,因此需要新的输入。 if the input is numeric the while condition will be false hence exit the while loop and will give you user input in stringIn variable. 如果输入为numeric ,则while条件为false因此退出while循环,并为您提供stringIn变量中的用户输入。

change in.next(); 改变in.next(); in while loop to stringIn= in.next(); 在while循环中stringIn= in.next(); and after while loop add stringOut = new StringBuilder(stringIn).reverse().toString(); 在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");
    }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM