我目前正在做一个练习(在任何人放弃之前不是家庭作业),我被困在问题的最后一部分。
问题是:
Write a program which will input a String from the keyboard, output the number of
seperate words, where a word is one or more characters seperated by spaces. Your
program should only count words as groups of characters in the rang A..Z and a..z
从我的代码中可以看出,我可以做第一部分没有问题:
导入 java.util.Scanner;
public class Exercise10 {
public static void main(String[] args) {
String input;
int counter = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your text: ");
input = keyboard.nextLine();
for(int i = 0; i < input.length(); i++){
if(input.charAt(i) == ' '){
counter++;
}
}
System.out.println(counter + 1);
keyboard.close();
}
}
然而,让我感到困惑的部分是:
Your program should only count words as groups of characters in the rang A..Z and
a..z
在这种情况下我应该怎么做?