想知道是否有人可以幫助我將「If else語句」更改爲「switch」 好吧c僞代碼是。 請幫忙。將「if else語句」更改爲switch語句
IF month is 1,2, or 3, season = "Winter
Else if month is 4, 5, or 6, season = "Spring"
Else if month is 7,8, 9, season = "Summer"
Else if month is 10,11, or 12, season = "Fall"
If season is "Winter", season = "Spring"
Else if season is "Spring", season = "Summer"
Else if season is "Summer", season = "Fall"
Else season = "Winter"
2.My代碼
import java.util.Scanner;
public class mylab
{
public static void main(String[] args)
{ Scanner in = new Scanner(System.in);
int month;
int day;
String season= "seasons";
System.out.print("type a two digit month");
System.out.print(" and day");
month = in.nextInt();
day = in.nextInt();
String fall = " fall";
String winter = " winter ";
String summer = " summer";
String spring = " spring";
System.out.print(" Month="+ month +" Day= "+day);
if(month <= 3)
{ System.out.println(" Winter");
season= winter; }
else if (month <=6)
{ System.out.println(" Spring ");
season=spring; }
else if (month<= 9)
{ System.out.println(" Summer ");
season= spring; }
else if (month<=12)
{ System.out.println(" Fall");
season= fall; }
3.我只需要第1部分更改爲switch語句。 這是我到目前爲止
switch(month)
{ case 1: season= " winter";if (month <= 3) ;break;
case 2: season= " spring"; if (month <= 6) ;break;
case 3: season = " summer"; if (month <= 9); break;
case 4: season= " fall"; if (month <= 12); break ;
}
,但它不工作。
這是行不通的,因爲這是完全INV alid語法。我鼓勵你看看這個[Java Trails](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)。 – Makoto
嗅覺像我的一個班級做作業... – mellowfish