2016-08-09 39 views
1

Ich habe eine letzte Aufgabe, an der ich jetzt ein wenig festhalte und ich suchte nach Hilfe. Gegenwärtig wird jedes Kriterium von der Eingabeaufforderung erfüllt, mit Ausnahme des letzten Teils (Art von), der 2 numerische Benutzereingaben benötigt und eine while-Schleife die Summe auf weniger als 7 bringt. Dies liegt daran, dass das Programm, das ich schreibe, benötigt wird eine Benutzereingabe und gibt den Tag zurück, den sie ausgewählt haben, den vorherigen Tag im Wochenkalender, den folgenden Tag auf den ursprünglichen zurückgegebenen Wert, und schließlich gibt der Benutzer eine Anzahl von Tagen ein, um den ursprünglichen Tag herauszufinden ist plus X Anzahl der Tage generiert, was dieser Tag der Woche ist. Ich habe den Code beigefügt. Irgendwelche guten Zeiger oder Hilfe werden sehr geschätzt. Danke, dass du dir die Zeit genommen hast, es dir anzusehen.Tage der Woche Java-Programm

//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; 
    } 
} 
+2

Nur damit Sie wissen, brauchen Sie keine 'while' Schleife, um Tage zu zählen. '(startDay + daysToAdd)% 7' gibt dir den DOW' daysToAdd' nach 'startDay'. – cHao

Antwort

0

Wie @cHao sagte, Sie leicht das die modulo (oder remainder) Operator (%) unter Verwendung tun könnte.

x = (x + y) % 7