使用工作日并找到未来的日子

时间:2019-06-20 12:10:16

标签: java arrays java.util.scanner

给出两个输入,我正在尝试寻找计算未来工作日的方法:

  1. 当前工作日(范围为0-6,其中0为星期日)。
  2. 从当前工作日开始执行多少计数(任意数量)?
  3. 如果用户以前从当前工作日开始= 3,并且 counts = 7。
  4. 然后我希望它会回到3,与14或21类似。
  5. 如何对此进行概括以使计数在此固定范围内 0-6没有退出吗?

我已经完成了一些code,发布在下面,

public class ThoseDays {

    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);
        System.out.print("Enter number between 0-6 : ");
        int startFromHere = obj.nextInt();

        System.out.print("Enter number to count position from " + startFromHere + " : ");
        int rotateFromHere = obj.nextInt(); 


        System.out.print( startFromHere +  rotateFromHere);

        obj.close();
    }
}

实际结果:

> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 10

预期结果:

> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 3

2 个答案:

答案 0 :(得分:3)

嗨,我建议您使用modulo轮换7天之后的日期。Another tutorial here

public class ThoseDays {

  public static void main(String[] args) {

  //Scanner implements AutoCloseable
  //https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
    try (Scanner obj = new Scanner(System.in)) { 
      System.out.print("Enter number between 0-6 : ");
      int startFromHere = obj.nextInt();

      System.out.print("Enter number to count position from " + startFromHere + " : ");
      int rotateFromHere = obj.nextInt();
      int absoluteNumber = startFromHere + rotateFromHere;
      System.out.println(absoluteNumber);
      int rotatedNumber = absoluteNumber % 7;
      System.out.println(rotatedNumber);
    }
  }
}

答案 1 :(得分:2)

代码:

import java.util.*;
public class ThoseDays {

    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);
    System.out.println("note : 0:sun 1-6:mon-saturday");
        System.out.println("Enter the number between 0-6: ");// 0:sun 1-6:mon-saturday

        int startFromHere = obj.nextInt();

        System.out.println("Enter the number to count position from " + startFromHere+ ": ");
        int rotateFromHere =obj.nextInt();
        if(rotateFromHere%7==0)
        {
            System.out.println(startFromHere);
        }
        if(rotateFromHere%7!=0)
        {
       int dayOfWeek=(startFromHere+(rotateFromHere%7));
       if(dayOfWeek>=7)
       {
           System.out.println((dayOfWeek%7));
       }
       else
       {
           System.out.print(dayOfWeek);
       }
        }


        obj.close();
    }
}

通过更改条件并使用模来尝试使用此代码,我得到了所有正确的结果

输出:
startFromHere = 3
从此处旋转= 7或14或21或7的倍数
给出与开始日期相同的日期

如果轮播日期是>开始日期
例如:

startFromHere = 3 //wednesday
rotateFromHere = 11

输出将为:0,表示星期日

检查此代码,如果有用,请给我评分。