質問

My code asks for the user to enter the day with an integer value ie Sunday = 0, Monday = 1, etc., then asks the user to input a number representing an offset day. From there the program finds the day corresponding to the offset number ie

Enter today's day: 0 //Sunday
Enter the number of days elapsed since today: 6
6 days from Sunday is Sunday

The problem with the above output is that since I'm using, "%6" to find the offset day, it skips one day for values 6.

Also, I can't figure a way to find the day for an entered negative offset value. ie

Enter today's day: 0 //Sunday
Enter the number of days elapsed since today: -2
-2 days from Sunday is Friday

Here's my code

import java.util.Scanner ;

public class test {

public static void main (String[] args) {

Scanner Input = new Scanner(System.in);
    System.out.print("Enter today's day: ");
        int today = Input.nextInt();

System.out.print("Enter the number of days elapsed since today: ");
    int offset = Input.nextInt ();
        int offsetDay = 0;
        if (offset >= 0)
            offsetDay = (today + offset)% 6 ; //to get remainder and make it vary from 0 - 6            
        else if (offset < 0)
            offsetDay = (today - offset)% 6 ;


String todayText = null ;
String offsetText = null;

//Converting input integers into days in text for variable today
switch (today) {
    case 0 : todayText = "Sunday" ; break;
    case 1 : todayText = "Monday"; break;
    case 2 : todayText = "Tuesday"; break;
    case 3 : todayText = "Wednesday";break;
    case 4 : todayText = "Thrusday"; break;
    case 5 : todayText = "Friday"; break;
    case 6 : todayText = "Saturday"; break;
}

//Converting input integers into days in text for variable offset
switch (offsetDay) {
    case 0 : offsetText = "Sunday" ; break;
    case 1 : offsetText = "Monday"; break;
    case 2 : offsetText = "Tuesday"; break;
    case 3 : offsetText = "Wednesday";break;
    case 4 : offsetText = "Thrusday"; break;
    case 5 : offsetText = "Friday"; break;
    case 6 : offsetText = "Saturday"; break;
}

System.out.println(offset + " days from " + todayText + " is "  + offsetText);

}

}

My last question is, how would I implement this using enumeration? Any thoughts?

役に立ちましたか?

解決

Please, try this code snippet, maybe this will help you:

public class Days
{

    enum DAY
    {
        MON("Monday"),
        TUE("Tuesday"),
        WED("Wednesday"),
        THU("Thursday"),
        FRI("Friday"),
        SAT("Saturday"),
        SUN("Sunday");

        private String name;

        private DAY(String name)
        {
            this.name= name;
        }

        @Override
        public String toString()
        {
            return this.name;
        }

        public DAY add(int days)
        {
            int posMod = (this.ordinal() + days) % values().length;
            if (posMod < 0)
            {
                posMod += values().length;
            }
            return DAY.values()[posMod];
        }
    }

    public static void main(String[] args)
    {
        DAY a = DAY.values()[0];
        System.out.println(a);
        System.out.println(a.add(-2));
        System.out.println(a.add(0));
        System.out.println(a.add(-8));
    }

}

他のヒント

You should use %7 instead of %6 as counting from 0-6 inclusive is 7.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top