문자열에서 사용자로부터 입력을 받고 있는데 반복하고 case 문을 사용하여 테스트하고 싶지만 작동하지 않습니다. 그 진술을 인쇄하지 않습니다.스위치 케이스를 사용하여 string을 char로 변환
import java.io.*;
import java.util.*;
public class fh3
{
public static void main(String args[])throws IOException
{
String sentence = "";
System.out.println("Enter the word : ");
Scanner scan = new Scanner(System.in);
String word = scan.next();
char[] chars = word.toCharArray();
for(int i = 0; i < word.length(); i++)
{
System.out.println("---" + chars[i]);
switch(chars[i])
{
case 0: sentence = " ";
System.out.println("B");
break;
case 1: sentence = "A";
break;
case 2: sentence = "B";
System.out.println("B");
break;
case 3: sentence = "C";
break;
}
sentence+=sentence;
System.out.println(sentence);
}
}
}
내가 그것을 "B" 하지만 난 잘못 무엇입니까
Enter the word :
20
---2
---0
등의 인쇄를 인쇄해야합니다 (20) 서재를 입력하면? 당신이 문자이 아닌 정수에 전환하고 있기 때문에
문자가 숫자가 아닌 영문자가 포함되어 있기 때문에 어떻게 작동 할 수 있습니까? – Prateek
@Prateek 예 thats는 문자 유형이 – SpringLearner
인 이유는 아니지만 괜찮은 이유는 값이 '0 ','1 '등입니다. 자신의 프로그램에 대한 맥락, 나는 당신의 코드가 틀렸다는 것을 말하지 않습니다. – Prateek