2013-10-24 3 views
1

문자열에서 사용자로부터 입력을 받고 있는데 반복하고 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) 서재를 입력하면? 당신이 문자이 아닌 정수에 전환하고 있기 때문에

답변

1

는 :

당신이 char 유형의 스위치를하고있는 때문에
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; 
} 
3

, 귀하의 경우는 동일해야한다. 귀하의 경우, 당신은 정수 값으로 사건을 부여하므로, 그것은 단지 일치하지 않습니다. '0'0

스위치가 char 받고있다
switch(chars[i]) { 
    case '0': // switch on char '0' and not integer 0. 
    case '1': // switch on char '1' and not integer 1. 
    case '2': // switch on char '2' and not integer 2. 
    ... 
} 
1

하지만 적절한 case 동일하지입니다 there.So의 인쇄에만이 문 System.out.println("---" + chars[i]); 두 번 (때문에 귀하의 경우 word.length() 반환 2)

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); 
    } 


} 

} 
+0

문자가 숫자가 아닌 영문자가 포함되어 있기 때문에 어떻게 작동 할 수 있습니까? – Prateek

+0

@Prateek 예 thats는 문자 유형이 – SpringLearner

+0

인 이유는 아니지만 괜찮은 이유는 값이 '0 ','1 '등입니다. 자신의 프로그램에 대한 맥락, 나는 당신의 코드가 틀렸다는 것을 말하지 않습니다. – Prateek

1

Java에서 char 유형은 Ascii table을 통해 int 유형에 매핑됩니다. 당신은 문자 '0' 아닌 NUL 문자를 확인하려면

따라서, 당신은 수행해야합니다

switch(chars[i]) { 
    case '0': // do the work 
    case '1': // do the work 
    // ... 
} 
2
import java.io.IOException; 
import java.util.Scanner; 

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(); 

     //Switch case needs you to compare the expression with constants hence the final keyword. 
     final char CHARONE = '1'; 
     final char CHARTWO = '2'; 
     final char CHARTHREE = '3'; 
     final char CHARFOUR = '4'; 

     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 CHARONE: 
        sentence = "A"; 
        break; 
       case CHARTWO: 
        sentence = "B"; 
        System.out.println("B"); 
        break; 
       case CHARTHREE: 
        sentence = "C"; 
        break; 

      } 
      sentence += sentence; 
      System.out.println(sentence); 
     } 
    } 
} 

당신은 지우기 .. 문자와 int로 비교하기 위해 노력하고 있었다?

+0

케이스 0을 수정해야합니다 .-) :-). 간과 한 – arjun

관련 문제