我需要编写符合这些准则的程序:
编写一个程序DayOfWeek.java,它将日期作为输入并打印日期所在的星期几。您的程序应该采用三个命令行参数:m(月),d(日)和y(年)。 1月份使用1次,2月份使用2次,依此类推。输出打印0表示星期日,1表示星期一,2表示星期二,依此类推。对于公历,使用以下公式(其中/表示整数除法):
y0 = y − (14 − m) / 12
x = y0 + y0/4 − y0/100 + y0/400
m0 = m + 12 × ((14 − m) / 12) − 2
d0 = (d + x + 31m0 / 12) mod 7
到目前为止,这是我写的
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean keepGoing = true;
while(keepGoing) {
System.out.println("Month");
int m = scanner.nextInt();
if (m < 1 || m > 12) {
System.out.println("Months are between 1 and 12");
continue;
}
System.out.println("Day");
int d = scanner.nextInt();
if (d < 1 || d > 31) {
System.out.println("Days are between 1 and 31");
continue;
}
System.out.println("Year");
int y = scanner.nextInt();
if (y < -10000 || y > 10000) {
System.out.println("Years are between -10000 and 10000");
continue;
}
int y0 = y - (14 - m) / 12;
int x = y0 + y0/4 - y0/100 +y0/400;
int m0 = m + 12 * ((14 - m) / 12) - 2;
int d0 = (d + x + 31 * m0 / 12) % 7;
boolean c = 0 <= d0 <= 6;
if (c) {
String b = "Sunday";
} else {
if (c) {
String b = "Monday";
} else {
if (c) {
String b = "Tuesday";
} else {
if (c) {
String b = "Wednesday";
} else {
if (c) {
String b = "Thursday";
} else {
if (c) {
String b = "Friday";
} else {
if (c) {
String b = "Saturday";
}
}
}
}
}
}
}
System.out.println("The day of the week is " + b);
}
}
}
当我尝试运行时,它会说
DayOfWeek.java:36: error: bad operand types for binary operator '<='
boolean c = 0 <= d0 <= 6;
^
first type: boolean
second type: int
DayOfWeek.java:66: error: cannot find symbol
System.out.println("The day of the week is " + b);
^
symbol: variable b
location: class DayOfWeek
2 errors
我真的不知道如何解决这个问题,有人可以帮助我。我对编码还很陌生。
答案 0 :(得分:-2)
第boolean c = 0 <= d0 <= 6;
行应为boolean c = 0 <= d0 && d0 <= 6;
查看java specs以了解如何使用关系运算符。
基本上1&lt; 0是boolean类型,你不能使用小于和大于布尔值。