Java(使用if else语句查找未来日期) [英] Java (Find the future date with if else statements)

查看:171
本文介绍了Java(使用if else语句查找未来日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我无法弄明白,谢谢:
编写一个程序,提示用户输入一周中今天的整数(星期日是0,星期一是1 ,. ..和星期六是6)。同时提示用户输入今天之后未来一天的天数并显示一周的未来日期。这是样本运行:
输入今天的日期:1
输入当天的天数从今天开始:3
今天是星期一,未来一天是星期四
我的尝试是:

I have a question that I can't figure it out , Thank you: Write a program that prompts the user to enter an integer for today's day of the week (Sunday is 0 ,Monday is 1 ,... and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week .Here is the sample run: Enter today's day: 1 Enter number of the day elapsed since today:3 Today is monday and the future day is thursday My try is:

Scanner input = new Scanner(System.in);

System.out.print("Enter today's day (0 - 6):  ");
int day = input.nextInt();

System.out.print("Enter the number of days elapsed since today:  ");
int elapsed = input.nextInt();

if(day == 0)
{
    System.out.println("Sunday");
}
if(day   == 1)
{
    System.out.println("Monday");
}
if(day ==  2)
{
    System.out.println("Tuesday");
}
if(day  == 3)
{
    System.out.println("Wednesday");
}
if(day  ==  4)
{
    System.out.print("Thursday");
}
if(day ==  5)
{
    System.out.print("Friday");
}
if(day  == 6)
{
    System.out.print("Saturday");
}

System.out.print("Today is " + day + " and the future day is " + elapsed);


推荐答案

问题来自一本名为的书Java编程简介由Y. Daniel Liang撰写。除了使用字符串类型,我相信在下一章中将介绍;我为本练习编写的解决方案仅使用到目前为止所教授的内容。

The question is from a book titled "Introduction to Java programming" by Y. Daniel Liang. Apart from using the string type, which I believe is covered in the next chapter; the solution I wrote for this exercise uses only what you have been taught so far.

import java.util.Scanner;

public class Exercise_03_06 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter today's day: ");
        int todaysDay = input.nextInt();

        System.out.print("Enter the number of days elapsed since today: ");
        int elapsedDays = input.nextInt();

        int futureDay = (todaysDay + elapsedDays) % 7;
        String day_of_week = "";

        switch (todaysDay) {
            case 0: day_of_week = "Sunday"; break;
            case 1: day_of_week = "Monday"; break;
            case 2: day_of_week = "Tuesday"; break;
            case 3: day_of_week = "Wednesday"; break;
            case 4: day_of_week = "Thursday"; break;
            case 5: day_of_week = "Friday"; break;
            case 6: day_of_week = "Saturday";
        }

        switch (futureDay) {
            case 0:
                System.out.println("Today is " + day_of_week + " and the future day is Sunday."); break;
            case 1:
                System.out.println("Today is " + day_of_week + " and the future day is Monday."); break;
            case 2:
                System.out.println("Today is " + day_of_week + " and the future day is Tuesday."); break;
            case 3:
                System.out.println("Today is " + day_of_week + " and the future day is Wednesday."); break;
            case 4:
                System.out.println("Today is " + day_of_week + " and the future day is Thursday."); break;
            case 5:
                System.out.println("Today is " + day_of_week + " and the future day is Friday."); break;
            case 6:
                System.out.println("Today is " + day_of_week + " and the future day is Saturday.");
        }
    }
}

输出:

Enter today's day: 0
Enter the number of days elapsed since today: 31
Today is Sunday and the future day is Wednesday.

注意:


  • 第一个switch语句为变量 day_of_week 分配一个类型字符串的日期,后者用于打印今天的日子。

  • The first switch statement assigns a day of type string to the variable day_of_week which is later used for printing "today's day".

要获得未来的日期,您必须找到当天总和的剩余部分以及经过的天数除以7。

To obtain the future day, you must find the remainder of the sum of today's day and the number of days elapsed divided by 7.

最后一个switch语句匹配一个案例编号,该案例编号与 futureDay 变量中存储的编号相同(通过执行上述数学运算获得)。 / p>

The last switch statement "matches" a case number that is identical to the number stored within the futureDay variable (which is obtained by performing the mathematical operation noted above).

这篇关于Java(使用if else语句查找未来日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆