2014-03-29 57 views
-1
import java.io.*; 

public class Color { 
    public void main() throws IOException { 
     BufferedReader br; 
     br = new BufferedReader(new InputStreamReader(System.in)); 

     System.out.println("Enter your choice"); 
     System.out.println("Enter V for Violet"); 
     System.out.println("Enter I for Indigo"); 
     System.out.println("Enter B for Blue"); 
     System.out.println("Enter G for Green"); 
     System.out.println("Enter Y for Yellow"); 
     System.out.println("Enter O for Orange"); 
     System.out.println("Enter R for Red"); 
     char choice; 
     choice = (char) br.read(); 
     switch (choice) { 
     case V: 
      System.out.println("The spectrum color is Violet"); 
      break; 
     case I: 
      System.out.println("The spectrum color is Indigo"); 
      break; 
     case B: 
      System.out.println("The spectrum color is Blue"); 
      break; 
     case G: 
      System.out.println("The spectrum color is Green"); 
      break; 
     case Y: 
      System.out.println("The spectrum color is Yellow"); 
      break; 
     case O: 
      System.out.println("The spectrum color is Orange"); 
      break; 
     case R: 
      System.out.println("The spectrum color is Red"); 
      break; 
     default: 
      System.out.println("Wrong Input"); 
     } 
    } 
} 

它說---- ---- cannot find symbol - variable v ......當我試圖在bluej中編碼它。我目前正在學習java編程,這是我第一個基本的java程序。說「找不到符號 - 變量V」

+1

你所有的選擇都是char,所以你需要把它寫成'case'V':'等等 – sakura

+0

根據命名約定,類名應該以大寫字母開頭('color '應該是'Color')。 – Tiny

回答

4

你應該儘量使你的信實際字符:

case 'V': 

     System.out.println("The spectrum color is Violet"); 

     break; 

你現在正在做的是檢查是否選擇是變量V,其中,當然不存在。

1

要在Java中使用字符文字,您需要使用單引號。此外,您可能想要接受大寫和小寫v。所以,你可以做這樣的:

switch (Character.toUpperCase(choice)) 
{ 
    case 'V': 

0

你應該比較字符。嘗試case 'V'

-2

檢查大小寫。 'V''v'...