2016-09-23 62 views
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; 

         } 
       } 
     } 
    } 
+0

请关闭所有括号 –

+0

''string“.equals(”other string“);'。 (https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – GOXR3PLUS

回答

0
final String[] parts = words.split(" "); 

if(parts[0].equals(parts[1]) && parts[1].equals(parts[2]) { 
    System.out.println("These words are the exact same"); 
} 

.equals字符串的内容进行比较。如果字符串1等于字符串2并且字符串2等于字符串3,则字符串1等于字符串3的传递属性。

相关问题