사용자가 두 단어를 쓰라고 할 때, 먼저 단어 (알파벳 순서)를 인쇄 할 수 있습니까? 그리고 스캔 한 단어에 특정 문자가 있는지 확인하는 방법은 무엇입니까?알파벳이 알파벳순으로 표시되고 문자열에 특정 문자가 있는지 확인하십시오.
예 : 사용자가 "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
마지막 두 연산자가 잘못되어 있으며이를 수정하는 데 올바른 방법이 필요합니다.
'WORD1 = 'z'' ... 당신은 그것에 대해 생각해야하고하지 왜, 당신이 무엇을 생각 . – Tom
@Tom Indeed. 그것도 컴파일해서는 안되며 오류가 정확히 무엇이 잘못되었는지 설명해야합니다. –
@Tom "Word1"(사용자가 입력 한 단어)에 'z'가 표시되는지 여부를 나타내는 부울 문을 작성하려고했습니다. 나는 그것에 대해 많이 생각했다. – Bader