Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
switch 作業 ~~~JAVA
1. 跟使用者要一個整數,利用 switch 指令判斷其為奇數或偶數
2. 向使用者要性別身高體重資料, 利用switch指令計算其體重是太重 剛好或太輕
3. 用switch將成績分群 90-99分A 80-89分B 70-79分C 60-69分D 60分以下F
1 Answer
- 1 decade agoFavorite Answer
看看是否符合妳的需求
有問題可以再提出
第一題:
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a;
System.out.print("Enter Number:");
a = Integer.parseInt(br.readLine());
a=a%2;
switch(a)
{
case 0: System.out.println("Even");
break;
case 1: System.out.println("Odd");
break;
}
}
catch(NumberFormatException ex)
{
System.out.println("Please enter Number");
}
catch(Exception ex)
{
}
第二題:
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int height ,weight;
System.out.print("Height:");
height = Integer.parseInt(br.readLine());
height=height*height;
System.out.print("Weight:");
weight = Integer.parseInt(br.readLine());
weight=weight*10000;
int bmi=weight/height;
int _bmi=bmi/10;
System.out.println("Your BMI: "+ bmi);
switch(_bmi)
{
case 1: if(bmi>=18)
System.out.println("Normal Range");
else
System.out.println("Too Thin");
break;
case 2:
if(bmi<=24)
System.out.println("Normal Range");
else
System.out.println("Too Fat");
break;
default: System.out.println("You must be Sick!!");
}
}
catch(NumberFormatException ex)
{
System.out.println("Please enter Number");
}
catch(Exception ex)
{
}
第三題:
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int score;
System.out.print("Enter Score:");
score = Integer.parseInt(br.readLine());
String grade="F";
switch(score/10)
{
case 9: grade="A";
break;
case 8:grade="B";
break;
case 7: grade="C";
break;
case 6: grade="D";
break;
}
System.out.println("You Grade: "+grade);
}
catch(NumberFormatException ex)
{
System.out.println("Please enter Number");
}
catch(Exception ex)
{
}
Source(s): 有點閒又不太閒的我