给出两个输入,我正在尝试寻找计算未来工作日的方法:
我已经完成了一些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
答案 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,表示星期日
检查此代码,如果有用,请给我评分。