这是一个我需要做的简单编码..问题是,在运行之后,它会显示例如“今天是2天,2天后是星期四”。而不是“今天是星期二,2天后是星期四。”
import java.util.Scanner;
公共类FindFutureDay {
public static void main(String[] args) {
//Variable
int day, next, future;
//Create scanner
Scanner input=new Scanner(System.in);
//Input day
System.out.println("Enter day today: ");
day=input.nextInt();
//Convert day to String
String Sunday = String.valueOf(0);
String Monday = String.valueOf(1);
String Tuesday = String.valueOf(2);
String Wednesday = String.valueOf(3);
String Thursday = String.valueOf(4);
String Friday = String.valueOf(5);
String Saturday = String.valueOf(6);
//Input number of days later
System.out.println("Number of days later: ");
next=input.nextInt();
//Close scanner
input.close();
//Compute future day
future=day+next;
//Select result to display
if (future%7==0){
System.out.println("Today is "+day+" and "+next+" day later is Sunday.");
} else if (future%7==1) {
System.out.printf("Today is "+day+" and "+next+" day later is Monday.");
} else if (future%7==2) {
System.out.printf("Today is "+day+" and "+next+" day later is Tuesday.");
} else if (future%7==3) {
System.out.println("Today is "+day+" and "+next+" day later is Wednesday.");
} else if (future%7==4) {
System.out.println("Today is "+day+" and "+next+" day later is Thursday.");
} else if (future%7==5) {
System.out.println("Today is "+day+" and "+next+" day later is Friday.");
} else if (future%7==6) {
System.out.println("Today is "+day+" and "+next+" day later is Saturday.");
} else {
System.out.println("Invalid!");
}
}
}
答案 0 :(得分:0)
你的问题"是你打印整数,你得到一个数字打印。 你可以修复它,例如通过添加函数getDay(day)而不是day。
private String GetDay (int day)
{
if (day == 1)
{
return "Sunday";
}
return null;
}
System.out.println("Today is "+GetDay (int day) +" and "+next+" day later is Sunday.");
相同的逻辑适用于下一个整数。
答案 1 :(得分:0)
似乎您从不使用星期日到星期六的变量,也不会将天数存储为字符串,而是相应整数的字符串表示形式。因此,您将永远无法打印“星期二”而不是“2”,因为您只需打印变量'day',即扫描的整数。
您应该存储一个地图并从其整数值中获取String表示。这是一个例子:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class FindFutureDay {
private static Map<Integer, String> weekDays = new HashMap<Integer, String>();
static {
weekDays.put(0, "Sunday");
weekDays.put(1, "Monday");
weekDays.put(2, "Tuesday");
weekDays.put(3, "Wednesday");
weekDays.put(4, "Thursday");
weekDays.put(5, "Friday");
weekDays.put(6, "Saturday");
}
public static void main(String[] args) {
//Variable
int day, next, future;
//Create scanner
Scanner input = new Scanner(System.in);
//Input day
System.out.println("Enter day today: ");
day = input.nextInt();
//Input number of days later
System.out.println("Number of days later: ");
next = input.nextInt();
//Close scanner
input.close();
//Compute future day
future = (day + next) % 7;
//Select result to display
System.out.println("Today is " + weekDays.get(day) + " and " + next + " day later is " + weekDays.get(future) + ".");
}
}
希望它有所帮助。