Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。

3年前关闭。





我已经为较大的程序编写了一个简单的测试程序,在该程序中,我需要通过从a到g的一系列字母来验证String。

下面的测试程序应该询问字母,然后在a-g范围内打印接受消息,否则说'oops'并再次询问。

最佳答案

试试这个代码:

public static void main(String... params) {
    Scanner s = new Scanner(System.in);
    Character minValue = 'a';
    Character maxValue = 'g';
    while (true) {
        System.out.print("enter a char between a-g: ");
        Character input = s.nextLine().charAt(0);
        if (input >= minValue && input <= maxValue) {
            System.out.println("Ok");
        } else {
            System.out.println("oops");
            System.exit(0);
        }
    }
}

关于java - Java-验证字母范围之间的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41147875/

10-10 00:27
查看更多