【问题标题】:Days of the Week Java ProgramJava 程序的星期几
【发布时间】:2016-08-09 06:40:51
【问题描述】:

我有一个最后的任务,我已经坚持了一段时间,我正在寻求帮助。目前,提示符满足每个条件,除了最后一部分(某种),它是采用 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;
   }
}

【问题讨论】:

  • 您知道,您不需要while 循环来计算天数。 (startDay + daysToAdd) % 7 将在 startDay 之后给你 DOW daysToAdd

标签: java while-loop days


【解决方案1】:

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

x = (x + y) % 7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2018-10-28
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多