![](/img/trans.png)
[英]How do I properly sort the characters of a String to compare to another String in Java?
[英]I want to convert series of characters into a string and compare with another string
樣本輸入 1:
Enter the first letter:R
Enter the second letter:A
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W
示例輸出 1:
RAINBOW
樣本輸入 2:
Enter the first letter:R
Enter the second letter:E
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W
示例輸出 2:
The spelling is wrong
代碼:
public class spellcheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String str1;
String str2 = "rainbow";
int i = 0;
String a[] = new String[7];
System.out.print("Enter the first letter:");
a[0] = scan.nextLine();
System.out.print("Enter the first letter:");
a[1] = scan.nextLine();
System.out.print("Enter the first letter:");
a[2] = scan.nextLine();
System.out.print("Enter the first letter:");
a[3] = scan.nextLine();
System.out.print("Enter the first letter:");
a[4] = scan.nextLine();
System.out.print("Enter the first letter:");
a[5] = scan.nextLine();
System.out.print("Enter the first letter:");
a[6] = scan.nextLine();
}
}
下一步應該怎么做?
String tmp = "";
for(int j = 0; j < a.length; j ++)
tmp += a[j];
System.out.println(tmp);
輸出 -
Enter the first letter:r
Enter the first letter:a
Enter the first letter:i
Enter the first letter:n
Enter the first letter:b
Enter the first letter:o
Enter the first letter:w
rainbow
為了進行比較,您可以使用內置的 String 函數 .equals -> tmp.equals("Otherstring")
char
數組而不是String
數組來存儲字母。char
數組中創建一個String
,然后比較內容。equalsIgnoreCase
而不是equals
以不區分大小寫的方式比較拼寫。SpellCheck
。請按以下步驟操作:
import java.util.Scanner;
public class SpellCheck {
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 < a.length; i++) {
str1 = str1 + a[i];
}
然后通過 str2.equals(str1) 進行比較
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.