如何计算名字的数量Java
How to count count the number of names Java
大家好。
我是 Java 的新手,目前正在学习字符串。
我有一个任务是统计名字的个数,名字的长度必须至少是两个,名字的第一个字母要大写,第二个字母要小写。
问题是我不知道如何使用 Character.isUpperCase(text.charAt(i)) 和 Character.isLowerCase(text.charAt(i + 1) ) 在同一个 if.
我会使用一些建议或提示。
class NameCounterTest {
public static void main(String[] args) {
// 1
System.out.println(new NameCounter().count("Mars is great planet"));
// 2
System.out.println(new NameCounter().count("Moon is near Earth"));
// 0
System.out.println(new NameCounter().count("SPACE IS GREAT"));
}
}
class NameCounter {
public int count(String text) {
String[] words = text.split(" ");
int wordLength = 0, counter = 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
wordLength = word.length();
if (wordLength >= 2 && Character.isUpperCase(text.charAt(i)))
counter++;
}
return counter;
}
}
Output:
1; //Should be 1;
1; //Should be 2;
3; //Should be 0;
您可以使用 word.charAt(0)
和 word.charAt(1)
来获取循环中每个单词的第一个和第二个字符。
class NameCounterTest {
public static void main(String[] args) {
// 1
System.out.println(new NameCounter().count("Mars is great planet"));
// 2
System.out.println(new NameCounter().count("Moon is near Earth"));
// 0
System.out.println(new NameCounter().count("SPACE IS GREAT"));
}
}
class NameCounter {
public int count(String text) {
String[] words = text.split(" ");
int wordLength = 0, counter = 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
wordLength = word.length();
if (wordLength >= 2 && Character.isUpperCase(word.charAt(0)) && Character.isLowerCase(word.charAt(1)))
counter++;
}
return counter;
}
}
大家好。 我是 Java 的新手,目前正在学习字符串。
我有一个任务是统计名字的个数,名字的长度必须至少是两个,名字的第一个字母要大写,第二个字母要小写。
问题是我不知道如何使用 Character.isUpperCase(text.charAt(i)) 和 Character.isLowerCase(text.charAt(i + 1) ) 在同一个 if.
我会使用一些建议或提示。
class NameCounterTest {
public static void main(String[] args) {
// 1
System.out.println(new NameCounter().count("Mars is great planet"));
// 2
System.out.println(new NameCounter().count("Moon is near Earth"));
// 0
System.out.println(new NameCounter().count("SPACE IS GREAT"));
}
}
class NameCounter {
public int count(String text) {
String[] words = text.split(" ");
int wordLength = 0, counter = 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
wordLength = word.length();
if (wordLength >= 2 && Character.isUpperCase(text.charAt(i)))
counter++;
}
return counter;
}
}
Output:
1; //Should be 1;
1; //Should be 2;
3; //Should be 0;
您可以使用 word.charAt(0)
和 word.charAt(1)
来获取循环中每个单词的第一个和第二个字符。
class NameCounterTest {
public static void main(String[] args) {
// 1
System.out.println(new NameCounter().count("Mars is great planet"));
// 2
System.out.println(new NameCounter().count("Moon is near Earth"));
// 0
System.out.println(new NameCounter().count("SPACE IS GREAT"));
}
}
class NameCounter {
public int count(String text) {
String[] words = text.split(" ");
int wordLength = 0, counter = 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
wordLength = word.length();
if (wordLength >= 2 && Character.isUpperCase(word.charAt(0)) && Character.isLowerCase(word.charAt(1)))
counter++;
}
return counter;
}
}