Java 我想将一系列字符转换成一个字符串,并与另一个字符串进行比较
示例输入1: 输入第一个字母:R 输入第二个字母:A 输入第三个字母:I 输入第四个字母:N 输入第五个字母:B 输入第六个字母:O 输入第七个字母:W 样本输出1: 彩虹 样本输入2: 输入第一个字母:R 输入第二个字母:E 输入第三个字母:I 输入第四个字母:N 输入第五个字母:B 输入第六个字母:O 输入第七个字母:W 样本输出2: 拼写错误 代码- 下一步该怎么办Java 我想将一系列字符转换成一个字符串,并与另一个字符串进行比较,java,Java,示例输入1: 输入第一个字母:R 输入第二个字母:A 输入第三个字母:I 输入第四个字母:N 输入第五个字母:B 输入第六个字母:O 输入第七个字母:W 样本输出1: 彩虹 样本输入2: 输入第一个字母:R 输入第二个字母:E 输入第三个字母:I 输入第四个字母:N 输入第五个字母:B 输入第六个字母:O 输入第七个字母:W 样本输出2: 拼写错误 代码- 下一步该怎么办 String tmp = ""; for(int j = 0; j < a.length; j ++) tmp +=
String tmp = "";
for(int j = 0; j < a.length; j ++)
tmp += a[j];
System.out.println(tmp);
要进行比较,可以利用内置的字符串函数.equals->tmp.equals(“其他字符串”)
要进行比较,可以利用内置的字符串函数.equals->tmp.equals(“其他字符串”)
char
数组而不是String
数组来存储字母char
数组中创建一个String
,然后比较内容李>
equalsIgnoreCase
而不是equals
以不区分大小写的方式比较拼写import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
String[] suffix = { "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th" };
Scanner scan = new Scanner(System.in);
String str;
String str2 = "rainbow";
char a[] = new char[str2.length()];
for (int i = 0; i < a.length; i++) {
do {
System.out.print(
"Enter the " + ((i + 1) + suffix[i % 100 > 9 && i % 100 < 13 ? 3 : i % 10]) + " letter: ");
str = scan.nextLine();
if (str.length() == 1) {
a[i] = str.charAt(0);
}
} while (str.length() != 1);
}
String str1 = new String(a);
if (str1.equalsIgnoreCase(str2)) {
System.out.println(str2.toUpperCase());
} else {
System.out.println("The spelling is wrong");
}
}
}
另一个示例运行:
Enter the 1st letter: R
Enter the 2nd letter: A
Enter the 3rd letter: I
Enter the 4th letter: N
Enter the 5th letter: B
Enter the 6th letter: O
Enter the 7th letter: W
RAINBOW
Enter the 1st letter: R
Enter the 2nd letter: E
Enter the 3rd letter: I
Enter the 4th letter: N
Enter the 5th letter: B
Enter the 6th letter: O
Enter the 7th letter: W
The spelling is wrong
char
数组而不是String
数组来存储字母char
数组中创建一个String
,然后比较内容李>
equalsIgnoreCase
而不是equals
以不区分大小写的方式比较拼写import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
String[] suffix = { "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th" };
Scanner scan = new Scanner(System.in);
String str;
String str2 = "rainbow";
char a[] = new char[str2.length()];
for (int i = 0; i < a.length; i++) {
do {
System.out.print(
"Enter the " + ((i + 1) + suffix[i % 100 > 9 && i % 100 < 13 ? 3 : i % 10]) + " letter: ");
str = scan.nextLine();
if (str.length() == 1) {
a[i] = str.charAt(0);
}
} while (str.length() != 1);
}
String str1 = new String(a);
if (str1.equalsIgnoreCase(str2)) {
System.out.println(str2.toUpperCase());
} else {
System.out.println("The spelling is wrong");
}
}
}
另一个示例运行:
Enter the 1st letter: R
Enter the 2nd letter: A
Enter the 3rd letter: I
Enter the 4th letter: N
Enter the 5th letter: B
Enter the 6th letter: O
Enter the 7th letter: W
RAINBOW
Enter the 1st letter: R
Enter the 2nd letter: E
Enter the 3rd letter: I
Enter the 4th letter: N
Enter the 5th letter: B
Enter the 6th letter: O
Enter the 7th letter: W
The spelling is wrong
for(int i=0;i
然后通过str2.equals(str1)比较(int i=0;i
然后通过str2.equals(str1)进行比较Ganesha Kolar-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。公认的答案有助于未来的访问者自信地使用解决方案。检查以了解如何做。Ganesha Kolar-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。公认的答案有助于未来的访问者自信地使用解决方案。检查以了解如何做。