遇到一致性问题

时间:2015-05-05 23:23:45

标签: java java.util.scanner

我的程序出现问题。也就是说,当我输入一个值来说明从现在开始的几天后,它有时会起作用,有时则不起作用。我不确定问题是什么......跟我一起,我还在学习。 提前致谢

import java.util.*;

public class pooped {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

        int day;
        int june;
        int dates;

        System.out.println(" Days of the week are numbered 0 - 6 " +
                "From Sunday to Saturday, enter a number now");

        day = console.nextInt();

        int kill = day;
        System.out.println("Enter the number of days forward: ");
        dates = console.nextInt();

        printday(day);
        day = addDay(day);
        printday(day);
        day = removeDay(day);
        printday(day);
        dates = count(dates);
        printday(kill + dates);
    }

    public static int count(int dates) {

        if (dates > 6){
            dates = (dates % 6);
        }

        System.out.println("That many days out is: ");
        return dates;
    }

    private static int addDay(int day) {
        day++;
        System.out.println("The next day is: ");
        if (day > 6) {
            day = 0;
        }
        return day;
    }

    private static int removeDay(int day) {
        day = day - 2;
        System.out.println("The previous day is: ");
        if (day == 0) {
            day = 6;
        }
        return day;
    }

    public static boolean isWeek(int day) {
        return day >= 0 && day <= 6;
    }

    public static void printday(int day) {

        switch (day)
        {
            case 0:
                System.out.println("Sunday");
                break;
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");

            default:
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你应该修改7而不是6.修改为6只会产生0-5的值。

dates %= 7;

因此,无论何时添加或减去应保持在0-6之间的变量,请执行上述mod, 7

完整来源

public class Main {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

        int day;
        int june;
        int dates;

        System.out.println(" Days of the week are numbered 0 - 6 "
                + "From Sunday to Saturday, enter a number now");

        day = console.nextInt();

        System.out.println("Enter the number of days forward: ");
        dates = console.nextInt();

        printday(day);
        printday(addDay(day));
        printday(removeDay(day));
        printday(day + count(dates));

    }

    public static int count(int dates) {
        dates %= 7;
        System.out.println("That many days out is: ");
        return dates;
    }

    private static int addDay(int day) {
        day++;
        day %= 7;
        System.out.println("The next day is: ");
        return day;

    }

    private static int removeDay(int day) {
        day--;
        day += 7;
        day %= 7;
        System.out.println("The next day is: ");
        return day;
    }

    public static boolean isWeek(int day) {
        return day >= 0 && day <= 6;
    }

    public static void printday(int day) {
        switch (day) {
        case 0:
            System.out.println("Sunday");
            break;
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        case 6:
            System.out.println("Saturday");
        default:
            break;
        }
    }

}