2015-05-19 35 views
0

所以這裏是代碼。這是一個日曆程序的框架,查找一個月和一年。我不確定爲什麼我們的老師想要命令行參數,所以現在忽略它,除非它影響我的代碼。在頁面底部,我將顯示輸出。當我收到一個輸出時,儘管在該方法中調用了用戶輸入,但第二種方法(即處理幾個月)的任何內容都不會顯示。我希望我的程序能夠首先詢問用戶想要查看的年份,然後查看該月份。我確定我犯了一些明顯的錯誤,但是有人能指出我的方向嗎?Java:我的語句int他主要的方法是打印,但沒有在我的其他方法

package calendardisplay; 

/** 
* 
* @author TheDancingPope 
*/ 
import java.util.Scanner; 

public class CalendarDisplay 

{ 
    static Scanner scan = new Scanner(System.in);  

    public static final String MONTH_NAME[] = {"","Jan","Feb","Mar", 
               "Apr", "May","June", 
               "Jul", "Aug", "Sep", 
               "Oct", "Nov", "Dec"}; 
    public static void outputMonthName(int month) 
    { 
    System.out.println(MONTH_NAME[month]); 
    } 

public static void main(String[] args) 
{ 

     for (int i = 1; i < MONTH_NAME.length; i++) 
     { 
      System.out.println("Month number:" + i + 
       "\nis Month Name:\n" + MONTH_NAME[i]); 
     } 





     //Processes command line argument 
     System.out.println("Number of command line arguments: " + args.length); 

     for(int i = 0; i < args.length; i++) 
     { 
      System.out.println("Argument " + i + ": " + args[i]); 
     } 
    //This next section handles the leap year scanner. Based on the 
    //calculation below, paired with the user input, I will be able to tell 
    //whether or not they picked a leap year. 

    System.out.println("Please choose a year"); 
    int yearChosen = scan.nextInt(); 

    boolean isLeapYear = ((yearChosen % 4 == 0) && (yearChosen % 100 !=0) 
      || (yearChosen % 400 == 0)); 
    System.out.println("You chose\n" + yearChosen); 
     if (isLeapYear) 
     { 
      System.out.println (yearChosen + "\n is a leap year."); 
     } else 
     System.out.println(yearChosen + "\n is not a leap year."); 

} 
//This is where the months are processed. In here, I will sort through 
//the months chosen via the user, and based on their month, I will bring up 
//a formatted calendar for that month, with the correct amount of days in it. 
public static int monthTime() 
{ 
    System.out.println("Please chose a month:\n"); 
    int monthChosen = scan.nextInt(); 
    String monthString; 
    switch (monthChosen) 
     { 
     case 1: monthString = "January"; 
       break; 
     case 2: monthString = "February"; 
       break; 
     case 3: monthString = "March"; 
       break; 
     case 4: monthString = "April"; 
       break; 
     case 5: monthString = "May"; 
       break; 
     case 6: monthString = "June"; 
       break; 
     case 7: monthString = "July"; 
       break; 
     case 8: monthString = "August"; 
       break; 
     case 9: monthString = "September"; 
       break; 
     case 10: monthString = "October"; 
       break; 
     case 11: monthString = "November"; 
       break; 
     case 12: monthString = "December"; 
       break; 
     default: monthString = "Invalid month"; 

     break; 
     } 
    System.out.println("You chose:\n" + monthString); 
    return monthChosen; 
    } 
} 

當我運行它,我得到了我的小NetBeans的盒子下面的輸出下面我的代碼:

run: 
Month number:1 
is Month Name: 
Jan 
Month number:2 
is Month Name: 
Feb 
Month number:3 
is Month Name: 
Mar 
Month number:4 
is Month Name: 
Apr 
Month number:5 
is Month Name: 
May 
Month number:6 
is Month Name: 
June 
Month number:7 
is Month Name: 
Jul 
Month number:8 
is Month Name: 
Aug 
Month number:9 
is Month Name: 
Sep 
Month number:10 
is Month Name: 
Oct 
Month number:11 
is Month Name: 
Nov 
Month number:12 
is Month Name: 
Dec 
Number of command line arguments: 0 
Please choose a year 
2015 
You chose 
2015 
2015 
is not a leap year. 
BUILD SUCCESSFUL (total time: 5 seconds) 

它給了我一個提示,當用戶在一年內進入,而不是月。我已經提示程序在該方法中初始化一個int掃描器,但是我什麼也沒得到。我在做什麼錯?

回答

2

默認情況下運行的唯一方法是Main方法。要調用其他方法,您必須在Main方法中包含對它們的調用。

2

您的monthTime()方法永遠不會被調用。您可能想在main方法結束時調用它。

1

你還沒有在你的main函數中調用函數monthTime(),請嘗試在選擇year之後調用它?

+0

感謝您接受我的回答:D –

相關問題