首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算星期几错误

计算星期几错误
EN

Stack Overflow用户
提问于 2015-03-21 02:43:18
回答 2查看 94关注 0票数 1

我写了这个程序,但输出不起作用。你能帮我找出错误在哪里吗?我必须使用这个代码,而不是互联网上的任何其他代码,因为我们必须根据我们从课程中理解的内容来构建它。我使用的是jgrasp。

代码语言:javascript
复制
----jGRASP exec: javac -g samooras.java

我得到的错误是

代码语言:javascript
复制
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.

代码:

代码语言:javascript
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-03-21 02:47:33

假设您使用该公式从您创建的数组中获取星期几,在该数组中,您将执行以下操作:

代码语言:javascript
复制
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);
    }
}
票数 4
EN

Stack Overflow用户

发布于 2015-03-21 02:57:57

在定义以下内容时:

代码语言:javascript
复制
String[] dayoftheweek={"Sat","Sun","Mon","Tues","Wed","Thur","Fri"};

星期几是一个数组(这就是[]的意思)。一个数组包含多个相同类型的值(在本例中为字符串)。为了访问数组的一个元素,我们使用一个索引(例如,星期几是字符串“sat.”)。

所以当你说:

代码语言:javascript
复制
dayoftheweek=((day+ ...

您正在计算“星期几”数组中星期几的索引。

首先,您不能使用相同的名称,因此,您应该具有以下内容:

代码语言:javascript
复制
int dayIndex = ((day+ ...

有了索引后,您需要将其应用于数组,以便获得实际的星期几字符串:

代码语言:javascript
复制
System.out.println("the day of the week is: " + dayoftheweek[dayIndex]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29173708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档