我有以下代碼使用Java中的字符串從switch語句
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,但我很好奇如何做以上的未來參考。
爲什麼不把sysout語句放在你的switch case中 – mhasan
在'case'塊中聲明變量有什麼意義。它們將被限制在這些塊的範圍內(即在這些代碼塊之外不可見)。什麼是dayFuture,哪裏在定義中。爲什麼不做簡單的int算術? –
對不起,這是我第一次使用本網站。我試圖把我的整個代碼放在一起,但它沒有很好的翻譯,所以我把它剪掉了。 – NewProgrammerMR