Reputation: 877
I am trying to find means to compute future weekdays, given two inputs:
I've already done some code
which is posted below,
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();
}
}
Actual result:
> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 10
Expected result:
> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 3
Upvotes: 1
Views: 180
Reputation: 21
code:
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();
}
}
try this code by changing conditions and using modulo I'm getting all correct results
output:
startFromHere = 3
rotate fromHere = 7 or 14 or 21 or multiple of 7
gives the same date as the start date
if rotate date is > start date
for ex:
startFromHere = 3 //wednesday
rotateFromHere = 11
output will be : 0 which means sunday
check this code and give me a rating if useful thanks.
Upvotes: 2
Reputation: 1573
Hi i suggest you just use a modulo to rotate the days after they reach 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);
}
}
}
Upvotes: 3