2016-09-23 1 views
2

다음 코드는 사용자 입력 문자열에서 세 단어를 알파벳순으로 표시합니다.문자열에서 3 개의 사용자 입력 단어가 동일한 지 확인하는 방법은 무엇입니까?

예 :

고양이 개미 개

출력 :

개미 고양이 개

내가 할 노력하고있어

, 그래서 그것을 만들 것입니다 3 개의 단어가 완전히 동일하면 "이 단어들 똑같은가 "또는 그 라인에있는 것.

나는 그 일을 어떻게해야하는지 잘 모르겠습니다. 어떤 도움이나 충고라도 감사 할 것입니다, 감사합니다!

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

'' ".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과 같습니다.

관련 문제