我写了这个程序,但输出不起作用。你能帮我找出错误在哪里吗?我必须使用这个代码,而不是互联网上的任何其他代码,因为我们必须根据我们从课程中理解的内容来构建它。我使用的是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);
}
}发布于 2015-03-21 02:47:33
假设您使用该公式从您创建的数组中获取星期几,在该数组中,您将执行以下操作:
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);
}
}发布于 2015-03-21 02:57:57
在定义以下内容时:
String[] dayoftheweek={"Sat","Sun","Mon","Tues","Wed","Thur","Fri"};星期几是一个数组(这就是[]的意思)。一个数组包含多个相同类型的值(在本例中为字符串)。为了访问数组的一个元素,我们使用一个索引(例如,星期几是字符串“sat.”)。
所以当你说:
dayoftheweek=((day+ ...您正在计算“星期几”数组中星期几的索引。
首先,您不能使用相同的名称,因此,您应该具有以下内容:
int dayIndex = ((day+ ...有了索引后,您需要将其应用于数组,以便获得实际的星期几字符串:
System.out.println("the day of the week is: " + dayoftheweek[dayIndex]);https://stackoverflow.com/questions/29173708
复制相似问题