我已经编写了这个程序,但输出不工作。你能帮我确定错误在哪里吗? 我必须使用此代码而不是来自互联网的任何其他代码, 因为我们必须根据我们从类(class)中理解的内容来构建它。 我正在使用 jgrasp。
----jGRASP exec: javac -g samooras.java
我得到的错误是
samooras.java:25: error: incompatible types: int cannot be converted to String[]
-2*(year/100))%7+7)%7+1;
^
1 error
----jGRASP wedge2: exit code for process is 1.
代码:
import java.util.Scanner;
public class samooras {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] dayoftheweek = {"Sat", "Sun", "Mon", "Tues", "Wed", "Thur", "Fri"};
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
dayoftheweek = ((day +
(13 * ((month + 9) % 12 + 1) - 1) / 5
+ year % 100
+ year % 100 / 4
+ year / 400
- 2 * (year / 100)) % 7 + 7) % 7 + 1;
System.out.println("the day of the week is: " + dayoftheweek);
}
}
最佳答案
假设您正在使用该公式从您创建的数组中获取星期几,您将在其中执行以下操作:
import java.util.Scanner;
public class samooras {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] dayoftheweekArray = {"Sat", "Sun", "Mon", "Tues", "Wed", "Thur", "Fri"};
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
int dayoftheweekNumber = ((day +
(13 * ((month + 9) % 12 + 1) - 1) / 5
+ year % 100
+ year % 100 / 4
+ year / 400
- 2 * (year / 100)) % 7 + 7) % 7 + 1;
String dayoftheweek = dayoftheweekArray[dayoftheweekNumber];
System.out.println("the day of the week is: " + dayoftheweek);
}
}
关于java - 计算星期误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29173708/