一周中的日子
Days of the week
我正在尝试制作一个程序,从用户输入中获取日期,然后告诉他们前一天和那天 after.The 用户还应该能够输入要添加的天数和程序那天应该输出。
示例用户输入 1 = 星期一,明天 = 2 星期二 昨天 = 3 星期日
如果用户说是星期一 (1) 并加上 12 天,则输出应该是星期六 (6)
问题是每当 "theWeekDay" 大于 7 时它什么都不输出,因为 TheDay();没有大于 7 的条件。请帮助我!
非常感谢!
import java.util.Scanner;
import java.util.Scanner;
public class Problem_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int theWeekDay;
System.out.println("What Day Is It?");
theWeekDay = input.nextInt();
Days one = new Days(theWeekDay);
System.out.println("Today It Is: ");
one.TheDay(theWeekDay);
System.out.println("Yesterday It Was: ");
one.PreviousDay(theWeekDay);
System.out.println("Tomorrow It Is: ");
one.NextDay(theWeekDay);
System.out.println("How Many Days To Add?");
int x = input.nextInt();
System.out.println("Now It Is: ");
one.AddedDays(x);
}
}
class Days {
private int theWeekDay;
public Days(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int getTheWeekDay() {
return theWeekDay;
}
public void setTheWeekDay(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int TheDay(int theWeekDay) {
// an arra days of week + then add days in it
if (theWeekDay == 0) {
theWeekDay = theWeekDay + 7;
}
if (theWeekDay == 1) {
System.out.println("Monday");
} else if (theWeekDay == 2) {
System.out.println("Tuesday");
} else if (theWeekDay == 3) {
System.out.println("Wednsday");
} else if (theWeekDay == 4) {
System.out.println("Thursday");
} else if (theWeekDay == 5) {
System.out.println("Friday");
} else if (theWeekDay == 6) {
System.out.println("Saturday");
} else if (theWeekDay == 7) {
System.out.println("Sunday");
}
return theWeekDay;
}
public int PreviousDay(int theWeekDay) {
theWeekDay = theWeekDay - 1;
return TheDay(theWeekDay);
}
public int NextDay(int theWeekDay) {
theWeekDay = theWeekDay + 1;
if (theWeekDay > 7) {
theWeekDay = 1;
}
return TheDay(theWeekDay);
}
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = theWeekDay + AddedDays;
return TheDay(theWeekDay);
}
}
您的 if else 必须涵盖所有情况....
在
之后添加一个else
else if(theWeekDay == 7){
System.out.println("Sunday");
}
类似于:
if(theWeekDay == 1){
System.out.println("Monday");
}else if(theWeekDay == 2){
System.out.println("Tuesday");
}else if(theWeekDay == 3){
System.out.println("Wednsday");
}else if(theWeekDay == 4){
System.out.println("Thursday");
}else if(theWeekDay == 5){
System.out.println("Friday");
}else if(theWeekDay == 6){
System.out.println("Saturday");
}else if(theWeekDay == 7){
System.out.println("Sunday");
}else{
System.out.println("Invalid input");
}
return theWeekDay;
如果您想假设大于 7 的值有效,您一定要使用模运算。像这样:
if(theWeekDay > 7) {
theWeekDay = theWeekDay % 7;
}
否则你应该抛出异常。
正如 user629735 所说,在你的公式中使用模数。
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = (theWeekDay + AddedDays) % 7;
return TheDay(theWeekDay);
}
我正在尝试制作一个程序,从用户输入中获取日期,然后告诉他们前一天和那天 after.The 用户还应该能够输入要添加的天数和程序那天应该输出。
示例用户输入 1 = 星期一,明天 = 2 星期二 昨天 = 3 星期日
如果用户说是星期一 (1) 并加上 12 天,则输出应该是星期六 (6)
问题是每当 "theWeekDay" 大于 7 时它什么都不输出,因为 TheDay();没有大于 7 的条件。请帮助我!
非常感谢!
import java.util.Scanner;
import java.util.Scanner;
public class Problem_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int theWeekDay;
System.out.println("What Day Is It?");
theWeekDay = input.nextInt();
Days one = new Days(theWeekDay);
System.out.println("Today It Is: ");
one.TheDay(theWeekDay);
System.out.println("Yesterday It Was: ");
one.PreviousDay(theWeekDay);
System.out.println("Tomorrow It Is: ");
one.NextDay(theWeekDay);
System.out.println("How Many Days To Add?");
int x = input.nextInt();
System.out.println("Now It Is: ");
one.AddedDays(x);
}
}
class Days {
private int theWeekDay;
public Days(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int getTheWeekDay() {
return theWeekDay;
}
public void setTheWeekDay(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int TheDay(int theWeekDay) {
// an arra days of week + then add days in it
if (theWeekDay == 0) {
theWeekDay = theWeekDay + 7;
}
if (theWeekDay == 1) {
System.out.println("Monday");
} else if (theWeekDay == 2) {
System.out.println("Tuesday");
} else if (theWeekDay == 3) {
System.out.println("Wednsday");
} else if (theWeekDay == 4) {
System.out.println("Thursday");
} else if (theWeekDay == 5) {
System.out.println("Friday");
} else if (theWeekDay == 6) {
System.out.println("Saturday");
} else if (theWeekDay == 7) {
System.out.println("Sunday");
}
return theWeekDay;
}
public int PreviousDay(int theWeekDay) {
theWeekDay = theWeekDay - 1;
return TheDay(theWeekDay);
}
public int NextDay(int theWeekDay) {
theWeekDay = theWeekDay + 1;
if (theWeekDay > 7) {
theWeekDay = 1;
}
return TheDay(theWeekDay);
}
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = theWeekDay + AddedDays;
return TheDay(theWeekDay);
}
}
您的 if else 必须涵盖所有情况....
在
之后添加一个elseelse if(theWeekDay == 7){
System.out.println("Sunday");
}
类似于:
if(theWeekDay == 1){
System.out.println("Monday");
}else if(theWeekDay == 2){
System.out.println("Tuesday");
}else if(theWeekDay == 3){
System.out.println("Wednsday");
}else if(theWeekDay == 4){
System.out.println("Thursday");
}else if(theWeekDay == 5){
System.out.println("Friday");
}else if(theWeekDay == 6){
System.out.println("Saturday");
}else if(theWeekDay == 7){
System.out.println("Sunday");
}else{
System.out.println("Invalid input");
}
return theWeekDay;
如果您想假设大于 7 的值有效,您一定要使用模运算。像这样:
if(theWeekDay > 7) {
theWeekDay = theWeekDay % 7;
}
否则你应该抛出异常。
正如 user629735 所说,在你的公式中使用模数。
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = (theWeekDay + AddedDays) % 7;
return TheDay(theWeekDay);
}