2
以下代码按字母顺序排列用户输入字符串中的三个单词。如何检查字符串中的三个用户输入词是否相同?
例子:
猫蚂蚁狗
输出:
蚂蚁猫狗
我想要做的,就是做起来很如果三个单词完全相同,它会打印出“这些单词是完全相同的“,或者是与此相关的东西。
我不太清楚如何去做这件事。任何帮助或建议,将不胜感激,谢谢!
final Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter three words : ");
final String words = keyboard.nextLine();
final String[] parts = words.split(" ");
System.out.print("In alphabetical order those are: ");
alphabetizing (parts);
for (int k = 0; k < 3; k++)
System.out.print(parts [k] + " ");
public static void alphabetizing(String x [])
{
int word;
boolean check = true;
String temp;
while (check)
{
check = false;
for (word = 0; word < x.length - 1; word++)
{
if (x [ word ].compareToIgnoreCase(x [ word+1 ]) > 0)
{
temp = x [ word ];
x [ word ] = x [ word+1];
x [ word+1] = temp;
check = true;
}
}
}
}
请关闭所有括号 –
''string“.equals(”other string“);'。 (https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – GOXR3PLUS