根据用户输入计算一天:始终在返回的同一天

时间:2015-11-13 20:57:45

标签: java switch-statement

我正在尝试根据当前日期以及用户输入的未来天数计算日历中的某一天。所以,例如,今天是我的计划中的星期五或第5天。如果用户提前15天进入,我希望它打印当天是星期六(从今天起15天)。不幸的是,无论前进多少天,我都将星期五作为日期结果。有人可以帮忙吗?谢谢。

到目前为止

代码:

import java.util.*;
import java.text.SimpleDateFormat;

public class DayCalculator {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    int userEntryInt;
    String dayName;
    String userEntry;
    String weekdayName = new SimpleDateFormat
        ("EEEE", Locale.ENGLISH).format(System.currentTimeMillis());

    System.out.println("Today is "+weekdayName+".");
    System.out.println("Please enter how many days in the past or future "+
            "of which you'd like to know the day.");

    userEntry = sc.next(); 
    userEntryInt = Integer.parseInt(userEntry);
    dayName = getDayNumber(weekdayName, userEntryInt);   


    System.out.println("Your selected day is a " + dayName +".");   
}  

//method to calculate new day based on user entry
public static String getDayNumber(String name, int userNumber)    
   {

    String dayNumber = "TEST"; 
    int dayResult = 0;
    int dayNumberInt;

    switch (name){

    case "Monday":
     dayNumber = "1";
     break;
    case "Tuesday":   
     dayNumber = "2";
     break;
    case "Wednesday":
     dayNumber = "3";
     break;
    case "Thursday" : 
     dayNumber = "4";
     break;
    case "Friday":    
     dayNumber = "5";
     break;
    case "Saturday":  
     dayNumber = "6";
     break;
    case "Sunday":    
     dayNumber = "7";
    }

    System.out.println(dayNumber); //test
    dayNumberInt = Integer.parseInt(dayNumber);  
    System.out.println("dayNumberInt is "+dayNumberInt);//test

    System.out.println("dayResult is "+dayResult);//test

    if(((dayNumberInt+userNumber)/7)<7)
    {    
        dayResult = dayNumberInt+dayResult;   
    }
    else if (((dayNumberInt+userNumber)/7)>7)
    {

        dayResult = dayNumberInt-dayResult;
    }

    if (dayResult <0)
    {
       dayResult = -dayResult; 
    }

    if (dayResult==0)
    {
        dayResult = dayNumberInt;
    }
    String dayNameResult="";

    switch (dayResult){

    case 1: dayNameResult = "Monday";
            break;
    case 2: dayNameResult = "Tuedsay";
            break;
    case 3: dayNameResult = "Wednesday";
            break;
    case 4: dayNameResult = "Thursday";
            break;
    case 5: dayNameResult = "Friday";
            break;
    case 6: dayNameResult = "Saturday"; 
            break;
    case 7: dayNameResult = "Sunday"; 
           break;
    }

     return (dayNameResult);
  }
 }

2 个答案:

答案 0 :(得分:0)

您要做的是拍摄当前的星期几并将其更改为一个数字,然后取出该数字并将其添加到用户提供的数字中。然后除以7并找到余数。

注意:

%是余数运算符。 10%3 = 1,5%3 = 2,100%10 = 0。

例如:

星期几是星期一,所以天数值为1.用户输入9,所以你将1和9加在一起得到10.剩下的10除以7是3所以星期几是星期三。

public static getDayNumber(String name, int userNumber){
    if(userNumber<1)
        return "Invalid Number";
    int dayNumber = 0;
    switch(name){
        case "Monday":
            dayNumber = 1;
        break;
        case "Tuesday":   
            dayNumber = 2;
        break;
        case "Wednesday":
            dayNumber = 3;
        break;
        case "Thursday" : 
            dayNumber = 4;
        break;
        case "Friday":    
            dayNumber = 5;
        break;
        case "Saturday":  
            dayNumber = 6;
        break;
        case "Sunday":    
            dayNumber = 7;
    }
    dayNumber = (dayNumber + userNumber)%7;

    switch (dayNumber){
        case 1: return "Monday";
                break;
        case 2: return "Tuedsay";
                break;
        case 3: return "Wednesday";
                break;
        case 4: return "Thursday";
                break;
        case 5: return "Friday";
                break;
        case 6: return "Saturday"; 
                break;
        case 7: return "Sunday"; 
               break;
        default: return "Invalid Day Provided";
    }
}

答案 1 :(得分:0)

使用哈希图

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Solution {

    public static void main(String[] args) {
         String resultDay=new Solution().solution("Sat",23);
         System.out.println(resultDay);

    }

    public String solution(String S, int K) {
        int day = getday(K);
        int cDay = listofdays.get(S);
        cDay = day + cDay;
        cDay = cDay % 7;
        return getDayOfWeek(cDay);
    }

    public static Map<String, Integer> listofdays;
    static {
        listofdays = new LinkedHashMap<>();
        listofdays.put("Sun", 1);
        listofdays.put("Mon", 2);
        listofdays.put("Tue", 3);
        listofdays.put("Wed", 4);
        listofdays.put("Thur", 5);
        listofdays.put("Fri", 6);
        listofdays.put("Sat", 7);
    }

    static int getday(int k) {
        return k % 7;
    }

    private String getDayOfWeek(int value) {
        for (Entry<String, Integer> entry : listofdays.entrySet()) {
            if (entry.getValue().equals(value)) {
                return entry.getKey();
            }
        }
        return "";
    }
}