2014-03-28 117 views
-1

好吧,我正在努力爲學校完成這個項目,它要求我做出幾個月的開關聲明。因此,如果用戶輸入1將打印出一月等等...我得到這個錯誤,它說:無法找到符號 - 可變一月Switch Statement Reverse

import java.util.*; 
/** 
* Outputs the number on a month name entered by the user. 
* 
* @author Jack 
* @version 1a 
*/ 
public class MonthSwitchReverse { 
    public static void main(String[] args) { 


     Scanner in = new Scanner (System.in); 

     int monthString; 
     String month; 



     System.out.println(" Jan = 1/Feb = 2/March = 3/April = 4/MAy = 5/June = 6"); 
     System.out.println(" July = 7/Aug = 8/Sep = 9/Oct = 10/Nov = 11/Dec = 12"); 
     System.out.print("Choose a month above and the system will print out the number assigned to that month: "); 
     month = in.next(); 


     switch (month) { 
      case "January": monthString = 1; 
        break; 
      case "February": monthString = 2; 
        break; 
      case "March": monthString = 3; 
        break; 
      case "April": monthString = 4; 
        break; 
      case "May": monthString = 5; 
        break; 
      case "June": monthString = 6; 
        break; 
      case "July": monthString = 7; 
        break; 
      case "August": monthString = 8; 
        break; 
      case "September": monthString = 9; 
        break; 
      case "October": monthString = 10; 
        break; 
      case "November": monthString = 11; 
        break; 
      case "December": monthString = 12; 
        break; 
      default: monthString = 404; 
        break; 
     } 
     System.out.println(monthString); 
    } 
} 

編輯:我固定它。謝謝你的幫助。

+1

好吧,你在哪裏* *認爲你已經定義了這些符號? (如果想法是爲用戶輸入*名稱*,爲什麼你用'int'作爲用戶輸入?) –

+0

我也不知道'January'是在哪裏定義的。你的意思是「一月」嗎?或者你的意思是「一個叫'January'的變量?」。 –

+0

我可以問爲什麼這個問題是downvoted?好吧,它是一個簡單的錯誤,但是它是一個有效的問題,帶有複製的代碼 - 我們可以看到他們嘗試了什麼。 –

回答

3

由於您正在傳遞一個整數來切換,所以每個情況應該是一個int值。

例如

case 1: monthString = "1"; 
    case 2: monthString = "2"; 

而同爲其餘所有。

推薦閱讀:Switch in java.