好吧,我正在编码该程序,它将丢弃所有非字母的字符。现在,我很难让程序识别出哪个。这是我做的一些代码。

System.out.println("Press enter every time, you type a new word, and press the period button to end it.");
Scanner question = new Scanner(System.in);
System.out.println("Press enter to continue, or tupe something random in");
String userInput = question.next();
while(!userInput.equals(".")){
    String userInput2 = question.next();
    System.out.println(userInput2);

    if(userInput2.equals("Stop")){
        break;
    }
}

最佳答案

您可以使用正则表达式删除所有不是小写或大写字母的字符:

String userInput2 = question.next();
userInput2 = userInput2.replaceAll("[^a-zA-Z]", "");
System.out.println(userInput2);

10-05 19:05