一周中的Java程序

时间:2016-08-09 06:40:52

标签: java while-loop days

我有一个最后的任务,我已经坚持了一段时间,我正在寻求帮助。目前,除了最后一部分(类型)采用2个数字用户输入并且有一个while循环使总数小于7之外,每个标准都得到满足。这是因为我写的程序需要采取一个用户的输入并返回他们选择的那天,它在每周日历上的前一天,第二天返回原始返回值,然后最后用户输入一个天数以便查找原始日期加上X天数会产生一周的那一天。我附上了代码。任何好的指针或帮助将不胜感激。感谢您花时间去看它。

//import library
import java.util.*;

//declare the class and make it public
public class Day 
{
//store days of the week
   private static final int SUNDAY = 0;
   private static final int MONDAY = 1;
   private static final int TUESDAY = 2;
   private static final int WEDNESDAY = 3;
   private static final int THURSDAY = 4;
   private static final int FRIDAY = 5;
   private static final int SATURDAY = 6;

//create a new scanner input
   static Scanner keyboard = new Scanner(System.in);

//creates a public method
   public static void main(String[] args)
   {
      //declare variables
      Day dy = new Day(Day.WEDNESDAY);
      int choice;
      //creates menu varible
      do
      {
         //creates the first selection menu like on page 529
         menu2();
         choice = keyboard.nextInt();
         System.out.println();

         switch (choice)
         {
            case 1:

               do 
               {
                  menu1();
                  choice = keyboard.nextInt();
                  System.out.println();
                  switch (choice)
                  {
                     case 0:
                        day = SUNDAY;
                        break;
                     case 1:
                        day = MONDAY;
                        break;
                     case 2:
                        day = TUESDAY;
                        break;
                     case 3:
                        day = WEDNESDAY;
                        break;
                     case 4:
                        day = THURSDAY;
                        break;
                     case 5:
                        day = FRIDAY;
                        break;
                     case 6:
                        day = SATURDAY;
                        break;
                     case 32: //used 32 since there are only ever 31 days 
                        System.exit(0);
                        break;

                     default:
                        System.out.println("Invalid Input");

                  }
               //output the calculated selections
                  System.out.print("The day you selected is: ");
                  dy.print();
                  System.out.println();

                  System.out.print("The next day is: ");
                  dy.setDay(dy.getNextDay());
                  dy.print();
                  System.out.println();

                  System.out.print("The previous is: ");
                  dy.setDay(dy.getPreviousDay());
                  dy.setDay(dy.getPreviousDay());
                  dy.print();
                  System.out.println();

                  System.out.print("How many days would you like to add (within a month's time)? ");
               //output added days

                  int days = keyboard.nextInt();
                  dy.setDay(days);
                  System.out.print("\nAdding " + days + " day(s) makes your new day: ");
                  while(day > 6)
                  {
                     choice + (day -= 7);
                  }

                  dy.print();
                  System.out.println();


               //program exit

               }
               while (choice != 32);

               break;
         //create a case for the testing method
            case 2:
               System.out.println("Test Data For Day Class");

               System.out.print("\nInitial day: ");
               dy = new Day(Day.SUNDAY);
               dy.print();

               System.out.print("\nNext day: ");
               dy.setDay(dy.getNextDay());
               dy.print();

               System.out.print("\nAdd 12 Days: ");
               dy.setDay(dy.addDays(12));
               dy.print();

               System.out.print("\nPrevious day: ");
               dy.setDay(dy.getPreviousDay());
               dy.print();

               System.out.print("\nAdd 3 days: ");
               dy.setDay(dy.addDays(3));
               dy.print();
               System.out.println("\n\n");

               break;
         //program exit
            case 32:
               System.exit(0);
               break;

            default:
               System.out.println("Invalid Input");
         }

      }
      while (choice != 32);
   }
//output the day selection menu
   public static void menu1()
   {
      System.out.println("Please Select Your Initial Day (or 32 to quit):");
      System.out.println(" 0: Sunday.");
      System.out.println(" 1: Monday.");
      System.out.println(" 2: Tuesday.");
      System.out.println(" 3: Wednesday.");
      System.out.println(" 4: Thursday.");
      System.out.println(" 5: Friday.");
      System.out.println(" 6: Saturday.");
      System.out.println("32: To quit the program.");
   }
//output the type selection menu
   public static void menu2()
   {
      System.out.println("Enter: ");
      System.out.println("1: To Enter Data into the program.");
      System.out.println("2: For Test Data.");
      System.out.println("32: To quit the program.");
   }




  //Stores the day 

   public static int day;


  //Set the day.

   public void setDay(int day) 
   {
      this.day = day;
   }


 //Output the day

   public void print() 
   {
      System.out.println(this.toString());
   }


 //Return the day.

   public int getDay() 
   {

      return day;
   }


//Return the next day.

   public int getNextDay() {
      return (day + 1) % 7;
   }
//override for the days of the week
   @Override
   public String toString() {
      switch (Day.day) {
         case SUNDAY:
            return "Sunday";
         case MONDAY:
            return "Monday";
         case TUESDAY:
            return "Tuesday";
         case WEDNESDAY:
            return "Wednesday";
         case THURSDAY:
            return "Thursday";
         case FRIDAY:
            return "Friday";
         case SATURDAY:
            return "Saturday";
      }
      return "";
   }


  //Return the previous day.

   public int getPreviousDay() 
   {
      if (day == 0)
         return (day + 6) % 7;
      else
         return (day - 1) % 7;
   }


 //Calculate and return the day by adding days

   public int addDays(int days) 
   {

      return (day += days);
   }


   //Add the constructors.

   public Day() 
   {
      this.day = SUNDAY;
   }

   public Day(int day)
   {
      this.day = day;
   }
}

1 个答案:

答案 0 :(得分:0)

正如@cHao所说,您可以使用modulo(或remainder)运算符(%)轻松完成此操作。

x = (x + y) % 7