我有以下代码
switch (dayCurrent) {
case 0:
String today_sun = "Today is Sunday";
break;
case 1:
String today_mon = "Today is Monday";
break;
case 2:
String today_tues = "Today is Tuesday";
break;
case 3:
String today_wed = "Today is Wednesday";
break;
case 4:
String today_thurs = "Today is Thursday";
break;
case 5:
String today_fri = "Today is Friday";
break;
case 6:
String today_sat = "Today is Saturday";
break;
// If statement for invalid entry
default: if (dayCurrent >= 7) {
System.out.println("Invalid day entered");
}
}
// Switch statements to display future day
if (dayCurrent < 7) {
switch (dayFinal) {
case 0:
String future_sun = dayFuture + " day(s) later it is Sunday";
break;
case 1:
String future_mon = dayFuture + " day(s) later is Monday";
break;
case 2:
String future_tues = dayFuture + " day(s) later is Tuesday";
break;
case 3:
String future_wed = dayFuture + " day(s) later is Wednesday";
break;
case 4:
String future_thurs = dayFuture + " day(s) later is Thursday";
break;
case 5:
String future_fri = dayFuture + " day(s) later is Friday";
break;
case 6:
String future_sat = dayFuture + " day(s) later is Saturday";
break;
如果我想创建一个显示两个开关结果的System.out.println,我将如何进行此操作? 我知道这个代码出现在这里之前:Java (Find the future date with if else statements),并且它更容易做System.out.print,但我很好奇如何进行上述操作以供将来参考。
答案 0 :(得分:1)
在代码中拼凑多个字符串时,将它们连接起来。在Java中有多种方法可以做到这一点。
+
符号组合了String
个对象。每个String
对象都是immutable,这意味着您不要更改(“变异”)其值。相反,新的实例与原始值和新添加的String对象相结合。
String message = "My name is ";
message = message + "Basil";
message = message + ".";
System.out.println( message ); // My name is Basil.
另一种方法是在实现append
接口的任何类的对象上调用Appendable
。
StringBuilder
是适合您情况的Appendable
实施。
StringBuilder message = new StringBuilder( "My name is " );
message.append( "Basil" );
message.append( "." );
System.out.println( message.toString() ); // My name is Basil.
警告:仅在thread-safety不成问题的情况下使用StringBuilder
。如果可以跨多个线程访问Appendable
对象,请改用StringBuffer
。
有关各种与字符串相关的对象的详细信息,并使用漂亮的图表来理解它们,请参阅问题的my Answer Exact difference between CharSequence and String in java。
DayOfWeek
DayOfWeek
enum包含七个实例,每周一天。获取今天的星期几意味着获取今天的日期,这需要一个时区。对于任何特定时刻,日期在全球范围内因地区而异。
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = DayOfWeek.from( today ); // `dow` is an *object*, not mere text like `Monday` or mere integer number like `1`.
使用此enum的对象而不仅仅是整数来表示星期几会使您的代码更加自我记录,提供type-safety并确保有效值。
问题代码中的主要问题是持有字符串的变量是在里面定义的 switch
case
;在退出switch
case
时该变量超出范围,并立即成为garbage collection的候选者(您的字符串对象消失并被销毁,从内存中清除,换句话说)您有机会连接或打印文本内容。
所以这段代码:
switch (dayCurrent) {
case 0:
String today_sun = "Today is Sunday";
break;
case 1:
String today_mon = "Today is Monday";
break;
…
......应该是:
String today = null;
switch (dayCurrent) {
case 0:
today = "Today is Sunday";
break;
case 1:
today = "Today is Monday";
break;
…
但是,更好的是,我们可以将String
对象替换为Appendable
:StringBuilder
。
StringBuilder
连接多个字符串时,在上下文中线程安全性不是问题,请使用StringBuilder
类。
请注意,我们在StringBuilder
块之外定义了名为message
的switch
对象。在switch
之外定义意味着对象存活,在切换工作之前,期间和之后存在。
StringBuilder message = new StringBuilder();
switch ( dow ) {
case DayOfWeek.MONDAY:
case DayOfWeek.TUESDAY:
case DayOfWeek.WEDNESDAY:
case DayOfWeek.THURSDAY:
case DayOfWeek.FRIDAY:
message.append( "Weekday - Working for a living. " );
break;
case DayOfWeek.SATURDAY:
case DayOfWeek.SUNDAY:
message.append( "Wohoo! Weekend… Party on Garth! " );
break;
default:
System.out.println("ERROR - Unexpectedly reached case DEFAULT on value for DayOfWeek 'dow'.");
}
代码的dayFuture
部分对我没有任何意义,而且似乎与跨多个switch
块拼凑文本的问题无关。所以我将使用无意义的whatever
开关。
switch ( whatever ) {
case thisThing:
message.append( "Use vanilla." );
break;
case thatThing:
message.append( "Use chocolate." );
break;
case theOtherThing:
message.append( "Use almond." );
break;
default:
System.out.println("ERROR - Unexpectedly reached case DEFAULT on value for whatever.");
}
现在我们已经构建了一个由多个组件组成的消息。我们可以将其输出到System.out
。
System.out.println( message.toString() ); // Outputs day-of-week text combined with flavor text.
平日 - 以谋生为生。使用巧克力。
至于将日期添加到日期以获取其他日期,只需调用plus…
或minus…
方法即可。
如果用户输入数字字符串,则解析为要添加的天数的数字。
Integer days = Integer.valueOf( "4" );
添加该天数。
LocalDate future = today.plusDays( days );
如果经过这段时间,请使用TemporalAmount
,例如Period
或Duration
对象,而不仅仅是整数。
Period period = Period.ofDays( days );
LocalDate future = today.plus( period );
要查看未来日期的星期几,请获取DayOfWeek
枚举对象。无需开关。
DayOfWeek dow = DayOfWeek.from( future );
您可以向DayOfWeek
询问其本地化名称。致电DayOfWeek::getDisplayName
。通过TextStyle
指定长度或缩写。并传递Locale
来指定翻译的人类语言和用于决定标点符号和部件排序等问题的文化规范。
String dowName =
dow.getDisplayName(
TextStyle.FULL_STANDALONE ,
Locale.CANADA_FRENCH );