java中的compareTo();charAt();方法 怎么用的
来源:百度知道 编辑:UC知道 时间:2024/09/25 11:26:03
请把下面代码中带注释的地方解释下谢谢 初学者 不知道什么意思。
public class StringCharacters {
public static void mian(String[] args) {
String text = "To be or not to be, that is the ques tion;"
+ "Whether 'tis nobler in the mind to suffer"
+ "the slings and arrows of outrageous fortune"
+ "or to take arms against a sea of troubles,"
+ "and by oppsing end them?";
int spaces = 0, vowels = 0, letters = 0;
int textLength = text.length();
for (int i = 0; i < textLength; i++) {
char ch = Character.toLowerCase(text.charAt(i));//??
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
}
if (Character.isLetter(ch)) { //??
letters++;
}
if (Character.isWhitespace(ch)) { //?
spaces++;
}
}
System.out.p
public class StringCharacters {
public static void mian(String[] args) {
String text = "To be or not to be, that is the ques tion;"
+ "Whether 'tis nobler in the mind to suffer"
+ "the slings and arrows of outrageous fortune"
+ "or to take arms against a sea of troubles,"
+ "and by oppsing end them?";
int spaces = 0, vowels = 0, letters = 0;
int textLength = text.length();
for (int i = 0; i < textLength; i++) {
char ch = Character.toLowerCase(text.charAt(i));//??
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
}
if (Character.isLetter(ch)) { //??
letters++;
}
if (Character.isWhitespace(ch)) { //?
spaces++;
}
}
System.out.p
String a = "123";
String b = "123";
String c = "456";
if (a.compareTo(b)) {
// a 和 b相等
} else {
// a 和 b不相等
}
a.charAt(1)返回一个Char型,值是'2';
compareTo(Character anotherCharacter)根据数字比较两个 Character 对象。
text.charAt(i)字符串转化成字符数组