2011-11-01 72 views
1

下面是代碼:方法問題使用switch語句,返回工作不

import java.util.*; 

public class dayName { 

    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Please enter the number to find out what day of the week it represents:"); 
     int number = in.nextInt(); 
     weekNumber(number); 
    } 

    public static String weekNumber(int number) 
    { 
     String dayNumber; 
     switch (number) 
     { 
     case 1: dayNumber = "Monday"; break; 
     case 2: dayNumber = "Tuesday"; break; 
     case 3: dayNumber = "Wednesday"; break; 
     case 4: dayNumber = "Thursday"; break; 
     case 5: dayNumber = "Friday"; break; 
     case 6: dayNumber = "Saturday"; break; 
     case 7: dayNumber = "Sunday"; break; 
     default: dayNumber = ""; break; 
     } 

     return dayNumber; 
    } 

} 

我的問題是,它編譯罰款,但是當你問它的數量並不返回任何東西,它應該返回的日子字符串取決於你輸入的數字。

請注意我正在學習Java。

+0

添加的System.out來顯示被傳遞到weekNumber()方法的數量。 – DwB

回答

6

weekNumber()只是返回值,但你不用做任何事情。

更改該行:

System.out.println(weekNumber(number)); 
1

如果你要求多少? 如果你問它的主要方法這行代碼:

weekNumber(number); 

沒有你的字符串分配到一個字符串變量。

試試這個在main方法

System.out.println(weekNumber(number)); 

希望這有助於!

2

你必須名稱寫入控制檯:

System.out.println(weekNumber(number));

+0

哇,三個在同一時間... – BenCole