【发布时间】:2017-01-31 17:42:49
【问题描述】:
以下代码按字母顺序排列用户输入字符串中的三个单词。
例子:
猫蚁狗
输出:
蚂蚁猫狗
我正在尝试做的是,如果这三个词完全相同,它会打印出“这些词完全相同”或类似的内容。
我不太确定该怎么做。任何帮助或建议将不胜感激,谢谢!
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");怎么样。 (stackoverflow.com/questions/513832/…)
标签: java string if-statement