這是一個家庭作業問題。我已經晚了5天,想不通我做錯了什麼。這是我在Java的第一學期,我的這個網站爲什麼我的主課不能在我的日曆類中看到數組
這裏是分配的第一篇文章..
創建一個叫Calendar
的類。該類應該包含一個名爲events
的變量,它是一個字符串數組。數組應該被創建以保存5個元素。使用一個常數值來指定數組大小。不要硬編碼數組大小。在類構造函數中初始化數組,以便每個元素包含字符串「 - 沒有計劃的事件 - 」。
該類應包含一個名爲CreateEvent
的方法。該方法應接受一個String參數,該參數包含一個單詞用戶事件和一個表示星期幾的整數參數。 Monday
應該用數字1
代表,而Friday
應該用數字5
代表。使用傳遞給方法的事件信息填充事件數組。雖然用戶意願輸入一個字的事件,每個事件串應當預先考慮下面的字符串與每個事件:
event_dayAppoinment: (where event_day is the day of the week)
例如,如果用戶輸入1和「醫生」,第一個數組元素應改爲: 週一預約:醫生
如果用戶輸入2和「PTA」,第二個數組元素應改爲: 週二任命:PTA
寫驅動程序(在一個單獨的類)創建並調用您的日曆類。然後使用循環來收集用戶輸入。詢問當天(作爲一個整數),然後詢問事件(作爲一個單詞字符串)。將整數和字符串傳遞給Calendar對象的CreateEvent
方法。用戶應該能夠輸入0-5個事件。如果用戶輸入-1
,循環應該退出並且您的應用程序應該以表格格式打印出所有事件。您的程序不應允許用戶在一週中的某天輸入無效值。除了每週的1 – 5
或-1
以外的任何輸入都將被視爲無效。
備註: 從用戶獲取整數時,您需要在掃描儀對象上使用nextInt()
方法。從用戶獲取字符串時,您需要在掃描儀對象上使用next()
方法。
這是到目前爲止我的代碼..
//DRIVER CLASS
/**
*
* @author Rocky
*/
//imports scanner
import java.util.Scanner;
//begin class driver
public class driver {
/**
* @paramargs the command line arguments
*/
//begin main method
public static void main(String[] args) {
//initiates scanner
Scanner userInput = new Scanner (System.in);
//declare variables
int dayOfWeek;
String userEvent;
//creates object for calender class
calendercalenderObject = new calender();
//user prompt
System.out.println("Enter day of week for your event in the following format:");
System.out.println("Enter 1 for Monday");
System.out.println("Enter 2 for Tuesday");
System.out.println("Enter 3 for Wednsday");
System.out.println("Enter 4 for Thursday");
System.out.println("Enter 5 for Friday");
System.out.println("Enter -1 to quit");
//collect user input
dayOfWeek = userInput.nextInt();
//user prompt
System.out.println("Please type in the name of your event");
//collect user input
userEvent = userInput.next();
//begin while loop
while (dayOfWeek != -1) {
//test for valid day of week
if ((dayOfWeek>=1) && (dayOfWeek<=5)){
//calls createEvent method in calender class and passes 2 variables
calenderObject.createEvent(userEvent,dayOfWeek);
} else {
//error message
System.out.println("You have entered an invalid number");
//user prompts
System.out.println("Press -1 to quit or enter another day");
System.out.println("Enter 1 for Monday");
System.out.println("Enter 2 for Tuesday");
System.out.println("Enter 3 for Wednsday");
System.out.println("Enter 4 for Thursday");
System.out.println("Enter 5 for Friday");
System.out.println("Enter -1 to quit");
//collect user input
dayOfWeek = userInput.nextInt();
//end data validity test
}
//end while loop
}
//prints array to screen
int i=0;
for (i=0;i<events.length;i++){
System.out.println(events[i]);
}
//end main method
}
}
/**
*
* @author Rocky
*/
//imports scanner
import java.util.Scanner;
//begin calender class
public class calender {
//creates events array
String[] events = new String[5];
//begin calender class constructor
public calender() {
//Initializes array
String[] events = {"-No event planned-","-No event planned-","-No event planned-","-No event planned-","-No event planned-"};
//end calender class constructor
}
//begin createEvent method
public String[] createEvent (String userEvent, int dayOfWeek){
//Start switch test
switch (dayOfWeek){
case 1:
events[0] = ("Monday Appoinment:") + userEvent;
break;
case 2:
events[1] = ("Tuesday Appoinment:") + userEvent;
break;
case 3:
events[2] = ("WednsdayAppoinment:") + userEvent;
break;
case 4:
events[3] = ("Thursday Appoinment:") + userEvent;
break;
case 5:
events[4] = ("Friday Appoinment:") + userEvent;
break;
default:
break;
//End switch test
}
//returns events array
return events;
//end create event method
}
//end calender class
}
小樣式評論:類名應該大寫。 ''驅動程序'不''驅動程序','日曆'不''日曆'等 –
'我已經晚了5天,無法弄清楚我做錯了什麼 - - 見墨菲定律...然後,看到墨菲定律... – Coffee