2013-10-24 44 views
1

我正在從字符串中的用戶輸入,我想迭代和測試使用case語句,但它不工作。它不打印報表。將字符串轉換爲字符與開關的情況下

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


} 

} 

如果我輸入20巢穴它應打印 「B」 但是它作爲

Enter the word : 
20 
---2 

---0 

那裏我得到錯誤的打印?

回答

1

因爲你在字符,不是整數切換:

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

既然你正在做交換機上char類型,你的情況應該是相同的。在你的情況下,因爲你把整個值作爲整數值,它只是不匹配。 「0」是不等於0

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

您的開關被接受char但沒有合適的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肯定這就是爲什麼案件是char類型的 – SpringLearner

+0

沒有這就是爲什麼罰款的情況下is'0' ,‘1’等IM聊天中的價值上下文到他的程序,即時通訊不會說你的代碼是錯誤的。 – 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); 
     } 
    } 
} 

你試圖比較字符詮釋..清除?

+0

我想你將需要更正案例0:太:-)。忽視 – arjun