我试图将System.out.print放在同一行。我希望案例只有几天(案例0:"星期日和#34;)所以我可以写System.out.println("今天是" +天+"和未来的日子是" + m1)但是当我尝试这个时,我得到案例编号而不是字符串(今天是0,将来的日子是0)。我认为与我的方式相比,编写逻辑的方法更好:
import java.util.*;
public class HomeWork3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Sun = 0, Mon = 1, Tue = 2, Wed = 3, Thurs = 4, Fri = 5, Sat = 6 ");
System.out.print("\nEnter today's number: ");
int day = input.nextInt();
System.out.print("Enter the number of days that elapsed since today: ");
int n1 = input.nextInt();
//String strD = Integer.toString(day);
switch (day){
case 0: System.out.println("Today is Sunday");
break;
case 1: System.out.println("Today is Monday");
break;
case 2: System.out.println("Today is Tuesday");
break;
case 3: System.out.println("Today is Wednesday");
break;
case 4: System.out.println("Today is Thursday");
break;
case 5: System.out.println("Today is Friday");
break;
case 6: System.out.println("Today is Saturday");
break;
}
int m1 = ((day + n1)% 7);
switch (m1){
case 0: System.out.println("The future day is Sunday");
break;
case 1: System.out.println("The future day is Monday");
break;
case 2: System.out.println("The future day is Tuesday");
break;
case 3: System.out.println("The future day is Wednesday");
break;
case 4: System.out.println("The future day is Thursday");
break;
case 5: System.out.println("The future day is Friday");
break;
case 6: System.out.println("The future day is Saturday");
break;
}
//String strD = Integer.toString(day);
//System.out.println(strD + " this might work " + n1);
}
}
输出:
太阳= 0,星期一= 1,星期二= 2,星期三= 3,星期四= 4,星期五= 5,星期六= 6 输入今天的电话号码:2 输入自今天起经过的天数:5 今天是星期二 未来的一天是星期天
答案 0 :(得分:2)
以下内容如何:
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int m1 = ((day + n1)% 7);
String output = String.format("Today is %s, the future day is %s", days[day], days[m1]);
System.out.println(output);
(显然你需要确保一天< 7)
答案 1 :(得分:1)
如何简单地使用内置的DayOfWeek
枚举:
int day = 4;
System.out.println("Today is " + DayOfWeek.of(day)
.getDisplayName(TextStyle.FULL, Locale.getDefault()));
输出:
Today is Thursday
但是如果你想使用switch语句,我会说,添加一个抽象层,以简化你需要解决的问题。例如制作一个方法,其中包含int
并以String
的形式返回星期几:
public static String getWeekDay(int dayNumber) {
switch(dayNumber) {
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
...
}
throw new IllegalArguemntException("Invalid day number: " + dayNumber);
}
并使用它来创建输出:
System.out.println("Today is " + getWeekDay(day));
答案 2 :(得分:1)
最简单的方法是将第一个println()
- switch
更改为print()
并为文字添加空格。
第二个版本是定义两个字符串并在switch
- 块中设置值:
String today;
switch (day){
case 0: today = "Sunday";
break;
依此类推
String futureday;
switch (m1){
case 0: futureday = "Sunday";
break;
等等。最后你有你想要的输出:
System.out.println("Today is "+ today + " and the future day is " + futureday);
但最优雅的方法是定义一个工作日数组:
String[] days = {"Sunday","Monday","Tuesday"};
因此,您可以删除switch
- 块并简单地写:
System.out.println("Today is "+ days[day] + " and the future day is " + days[m1]);
提示:您必须初始化day
和futureday
。你应该检查,day
是< 7防止IndexOutOfBounds-Exception。
答案 3 :(得分:0)
因为你需要关联int和String,你可以使用地图
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//add all days into the map with their key (number)
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "Sunday");
map.put(1, "Monday");
map.put(2, "Tuesday");
map.put(3, "Wednesday");
map.put(4, "Thursday");
map.put(5, "Friday");
map.put(6, "Saturday");
//Printing all days
for(int key : map.keySet()){
System.out.print(key+"="+map.get(key)+", ");
}
System.out.print("\nEnter today's number: ");
int day = input.nextInt();
System.out.print("Enter the number of days that elapsed since today: ");
int n1 = input.nextInt();
n1 = ((day + n1)% 7);
System.out.println("Today is "+map.get(day) + ", the future day is " + map.get(n1));
}
它允许您取回与使用rhe scanner输入的键相对应的值