首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java while循环中的用户验证测试

Java while循环中的用户验证测试
EN

Stack Overflow用户
提问于 2018-09-27 09:11:46
回答 2查看 51关注 0票数 0

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

代码语言:javascript
复制
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");
}

    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-27 10:11:42

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

代码语言:javascript
复制
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");
    }

        }
    }
票数 0
EN

Stack Overflow用户

发布于 2018-09-27 09:27:07

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

代码语言:javascript
复制
    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变量中提供用户输入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52528031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档