private int calculateWeekDay() {
if (this.month == 1 || this.month == 2) {
this.month += 12;
this.year -= 1;
}
int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % 7;
String[] weekDay = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
int [] Array = new int[weekDay.length];
for(int i =0 ; i < weekDay.length ; i++){
Array[i] = Integer.parseInt(weekDay[i]);
}
return h;
}
我真的不知道错误在哪里,调试时得到的全部是这个错误:线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:“ Monday”在java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)在java.lang.Integer.parseInt(Integer.java:580)在java.lang.Integer.parseInt(Integer.java:615)在javaapplication22.JavaApplication22 $ Zeller.calculateWeekDay(JavaApplication22.java:112)在javaapplication22.JavaApplication22 $ Zeller.toString(JavaApplication22.java:125)在javaapplication22.JavaApplication22.main(JavaApplication22.java:17)C:\ Users \ salee \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:返回的Java:1失败(总时间:5秒)
这就是整个代码,如果它有助于更多地发现问题:
package javaapplication22;
import java.util.Scanner;
public class JavaApplication22 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Zeller [] obj = new Zeller[5];
for (int i = 1 ; i <= obj.length ; i++){
System.out.print("Enter Valid year (>0) - Valid month [1-12] and Valid day [1-31] for Object " + (i) + ":");
int year = scan.nextInt();
int month = scan.nextInt();
int day = scan.nextInt();
obj[i] = new Zeller(year , month , day);
System.out.println(obj[i].toString());
}
}
public static class Zeller {
private int year;
private int month;
private int day;
private String weekDay;
private static int countLeapYears;
public Zeller() {
year = -1;
month = -1;
day = -1;
}
public Zeller(int y, int m, int d) {
this.year = y;
this.month = m;
this.day = d;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String getWeekDay() {
return weekDay;
}
public static int getCountLeapYears() {
return countLeapYears;
}
public void setYear(int y) {
if (y > 0) {
System.out.println(y);
} else if( y <= 0){
System.out.println(this.year);
}
}
public void setMonth(int m) {
if (m >= 1 && m <= 12) {
System.out.println(m);
} else if (!(m >= 1 && m <=12 )){
System.out.println(this.month);
}
}
public void setDay(int m) {
if( m == 1 || m == 3 ||m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
this.day = 31;
System.out.println(this.day );
}else if( m == 4 || m == 6 || m == 9 || m == 11){
this.day = 30;
System.out.println(this.day);
}else if(m == 2){
if(this.checkLeapYear(year) == true){
this.day = 29;
System.out.println(this.day);
}else {
this.day = 28;
System.out.println(this.day);
}
}else{
System.out.println(this.day);
}
}
private int calculateWeekDay() {
if (this.month == 1 || this.month == 2) {
this.month += 12;
this.year -= 1;
}
int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % 7;
String[] weekDay = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
int [] Array = new int[weekDay.length];
for(int i =0 ; i < weekDay.length ; i++){
Array[i] = Integer.parseInt(weekDay[i]);
}
return h;
}
public boolean checkLeapYear(int year) {
return true == (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
@Override
public String toString() {
if(this.year != -1 || this.month !=0 ||this.day !=0){
return (" In " + this.getYear() + " / " + this.getMonth() + " / " + this.getDay() + " the day was: " + calculateWeekDay());
}
return ("The Date in this Object was input wrong");
}
}
}
}
谢谢。
尝试一下
if (this.month == 1 || this.month == 2)
{
this.month = (this.month == 1) ? 13 : 14;
this.year--;
}
int h = (this.day + (26 * (this.month + 1)) / 10 + (this.year % 100) + (this.year % 100) / 4 + (this.year / 100) / 4 + 5 * (this.year / 100)) % 7;
1:周日2:星期一3:星期二4:星期三5:星期四6:星期五7:星期六
[最有可能,您的代码应该计算星期几的String
名称:
static final String[] WEEKDAY = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
private String calculateWeekDay() {
if (this.month == 1 || this.month == 2) {
this.month += 12;
this.year -= 1;
}
int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % WEEKDAY.length;
return weekDay[h];
}
但是您可能会遗漏其他内容,因为对于今天的日期2020-04-29(星期三),它返回Sunday