2014-10-29 2 views
-2

사용자가 두 단어를 쓰라고 할 때, 먼저 단어 (알파벳 순서)를 인쇄 할 수 있습니까? 그리고 스캔 한 단어에 특정 문자가 있는지 확인하는 방법은 무엇입니까?알파벳이 알파벳순으로 표시되고 문자열에 특정 문자가 있는지 확인하십시오.

예 : 사용자가 "Word"와 "Apple"을 쓴 경우이 두 단어를 사전 순으로 인쇄 할 수 있습니다. 또한, 문자 'z'가 단어에 나타나는지 아닌지를 확인하는 프로그램을 작성했는데, 무엇이 잘못되었는지는 알지 못합니다. 여기 내 프로그램은 다음과 같습니다.

import java.util.*; 
public class Pr7{ 
    public static void main(String[] args){ 

    //print two words and read them.. 

    Scanner scan = new Scanner (System.in);  
    String Word1; 
    String Word2; 

    System.out.println(); 

    System.out.print("* Please write one word: "); 
     Word1 = scan.nextLine(); 
    System.out.print("* Please write one word: "); 
     Word2 = scan.nextLine(); 

    //Prints which word has more characters in it.. 

    if (Word1.length() > Word2.length()) 
     System.out.println("- " + "(" + Word1 + ")" + " has more characters."); 

    else if (Word2.length() > Word1.length()) 
     System.out.println("- " + "(" + Word2 + ")" + " has more characters."); 
    else 
     System.out.println("- " + "(" + Word1 + ")" + " has equal characters with " + "(" + Word2 + ")"); 

    //Prints which word comes first (alphabetically).. *** WORNG *** 

    char ch; 
    int compare = Word1.compareTO(Word2); 

    if (compare < 0) 
     System.out.print(compare) 
    else 
     if (compare > 0) 
    a is larger 
    }else 
    { 

    //Prints whether the letter 'z' appears in either word.. *** WRONG *** 

    if (Word1 = 'z') 
    System.out.print("- Letter 'z' appears in the first word."); 
    else if (Word2 = 'z') 
    System.out.print("- Letter 'z' appears in the second word."); 
    else 
    System.out.print("- Letter 'z' doesn't appears in either word."); 

    }//main 
}//Pr7 

마지막 두 연산자가 잘못되어 있으며이를 수정하는 데 올바른 방법이 필요합니다.

+0

'WORD1 = 'z'' ... 당신은 그것에 대해 생각해야하고하지 왜, 당신이 무엇을 생각 . – Tom

+0

@Tom Indeed. 그것도 컴파일해서는 안되며 오류가 정확히 무엇이 잘못되었는지 설명해야합니다. –

+0

@Tom "Word1"(사용자가 입력 한 단어)에 'z'가 표시되는지 여부를 나타내는 부울 문을 작성하려고했습니다. 나는 그것에 대해 많이 생각했다. – Bader

답변

1

두 단어를 정렬하려면 String.compareTo()을 사용하십시오. 목록에 추가하는 것이 더 쉽고 Collections.sort()을 사용하여 목록을 분류하는 것이 더 쉽습니다. 문자열은 자연스럽게 알파벳 순으로 정렬됩니다.

문자열에 특정 문자가 포함되어 있는지 확인하려면 String.indexOf('z')을 사용하고 반환 값이 일치하지 않음을 나타내는 -1인지 테스트하십시오. 또는 톰이 제안한대로 String.contains("z")을 사용할 수 있습니다. "z" 문자열 대신 'z' 문자열을 전달해야합니다. 나는 그것을 해결하는 방법이있다

+0

내 프로그램에 변경 사항을 적용하면 이해하기가 훨씬 쉬울 것이므로 잘못된 내용을 알고 있습니다. – Bader

+1

@Bader 예, 그렇습니다. 그러나 당신이 나의 힌트를 사용하고 그것을 스스로 해결한다면 당신은 언어에 관해 더 많은 것을 배울 것입니다. –

+1

Duncan, 'z'가 문자열의 일부인지 확인하는 방법으로'String.contains()'메소드를 포함 할 수도 있습니다. – Tom

0

..

import java.util.*; 
 
public class Pr6{ 
 
    public static void main(String[] args){ 
 
    Scanner scan = new Scanner (System.in);  
 
    String word1; 
 
    String word2; 
 
    String sentence1; 
 
    String sentence2; 
 
    
 
    System.out.print("* Please enter a word: "); 
 
    word1 = scan.nextLine(); 
 
    System.out.print("* Please enetr a word: "); 
 
    word2 = scan.nextLine(); 
 
    
 
    if (word1.length() > word2.length()) 
 
     System.out.print("The first word has more characters than the second word"); 
 
    else if (word1.length() < word2.length()) 
 
     System.out.print("The second word has more characters than the first word"); 
 
    else 
 
     System.out.print("The characters in both words are equal"); 
 
    
 
    System.out.println(); 
 
    
 
    if (word1.toLowerCase().charAt(0) < word2.toLowerCase().charAt(0)) 
 
     System.out.println(word1 + " (comes first alphabetically)"); 
 
    else if (word1.toLowerCase().charAt(0) > word2.toLowerCase().charAt(0)) 
 
     System.out.println(word2 + " (comes first alphabetically)"); 
 
    
 
    if (word1.toLowerCase().indexOf('z') >= 0) 
 
     System.out.println("Letter 'z' appears in the first word."); 
 
    else 
 
     System.out.println("Letter 'z' doesn't appear in the first word."); 
 
    if (word2.toLowerCase().indexOf('z') >= 0) 
 
     System.out.println("Letter 'z' appears in the second word."); 
 
    else 
 
     System.out.println("Letter 'z' doesn't appear in the second word."); 
 
    
 
    System.out.print("* Please enter a sentence: "); 
 
    sentence1 = scan.nextLine(); 
 
    System.out.print("* Please enter a sentence: "); 
 
    sentence2 = scan.nextLine(); 
 
    
 
    System.out.println("The first sentence has " + sentence1.length() + " characters"); 
 
    System.out.println("The second sentence has " + sentence2.length() + " characters"); 
 
    
 
    if (sentence1.toLowerCase().indexOf(" the ") >= 0) 
 
     System.out.println("Substring 'the' appears in the first sentence."); 
 
    else 
 
     System.out.println("Substring 'the' doesn't appear in the first sentence."); 
 
    if (sentence2.toLowerCase().indexOf(" the ") >= 0) 
 
     System.out.println("Substring 'the' appears in the second sentence."); 
 
    else 
 
     System.out.println("Substring 'the' doesn't appear in the second sentence."); 
 
    
 
    if (sentence1.length() >= 11) 
 
     System.out.println(sentence1.replace(sentence1.substring(8, 10), "the")); 
 
    if (sentence2.length() >= 11) 
 
     System.out.println(sentence2.replace(sentence2.substring(8, 10), "the")); 
 
     
 
    }//main 
 
}//Pr6

관련 문제