我目前正在开设一门软件开发课程并且已经获得了一个有点麻烦的作业,如果有人可以提供帮助,我会非常感激...这就是它在java中的任务:
编写一个可以访问两个数组的程序。 一个数组将包含月份的名称,另一个数组包含每个月的天数。
当用户输入月份编号时,程序将返回月份名称和天数....
这是我到目前为止输入的代码,有点卡在最后......
int number;
number = Integer.parseInt(jTextField1.getText());
int [] Days;
Days = new int [12];
Days [0] = 31;
Days [1] = 28;
Days [2] = 31;
Days [3] = 30;
Days [4] = 31;
Days [5] = 30;
Days [6] = 31;
Days [7] = 31;
Days [8] = 30;
Days [9] = 31;
Days [10] = 30;
Days [11] = 31;
jTextField3.setText(String.valueOf(Days));
String [] Month;
Month = new String [12];
Month [0] = "January";
Month [1] = "February";
Month [2] = "March";
Month [3] = "April";
Month [4] = "May";
Month [5] = "June";
Month [6] = "July";
Month [7] = "August";
Month [8] = "September";
Month [9] = "October";
Month [10] = "November";
Month [11] = "December";
jTextField2.setText(String.valueOf(Month));
答案 0 :(得分:4)
更好的版本将使用Month类:
public class Month {
private String name;
private int days;
public Month() {}
public Month(String name, int days) {
this.name = name;
this.days = days;
}
public String getName() {
return name;
}
public String getDays() {
return days;
}
}
Month[] months = new Month[12];
months[0] = new Month("January", 31);
// ...
int n;
// ...user read from keyboard n from 1 to 12 month;
// set month info
jTextField.setText("In " + months[n - 1].getName() + " are " + months[n - 1].getDays() + " days!");
答案 1 :(得分:0)
如果number = Integer.parseInt(jTextField1.getText());
是有效输入(1到12之间),那么您可以执行:Days [number]
以将该月的日期作为整数和Month [number]
来获取月份名称作为一个字符串。
答案 2 :(得分:0)
假设用户输入是介于1到12之间的数字(1月由1表示,12月由12表示),则月份名称由Month[number - 1]
给出,月份的日期由Days[number -1]
< / p>