標題:
switch 判斷式 (二)
[打印本頁]
作者:
鄭繼威
時間:
2022-4-19 23:20
標題:
switch 判斷式 (二)
本帖最後由 鄭繼威 於 2022-5-11 19:54 編輯
利用 switch 判斷式,設計一成績分級程式,分級方式如下:
90分~100分 優等
80分~89分 甲等
70分~79分 乙等
60分~69分 丙等
0分~59分 不及格
不在以上範圍 輸入錯誤
利用 ... 代表一個範圍,就像<=
記得空格哦
ex:1到5
1<=x<=5 就是 1 ... 5
[使用者介面如下]
請輸入你的成績: 77
乙等!
請輸入你的成績: 101
輸入錯誤!
[attach]13005[/attach]
用switch-case判斷
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
//宣告變數
//變數型態 變數名字
int score;
cout<<"請輸入你的成績:";
cin>>score;
//判斷成績
switch(score){
//case1
//90~100 -> 90<=score<=100
case 90 ... 100:
cout<<"優等"<<endl;
break;
//case2
//80~89 -> 80<=score<=89
case 80 ... 89:
cout<<"甲等"<<endl;
break;
//case3
//70~79 -> 70<=score<=79
case 70 ... 79:
cout<<"乙等"<<endl;
break;
//case4
//60~69 -> 60<=score<=69
case 60 ... 69:
cout<<"丙等"<<endl;
break;
//case5
//0~59 -> 0<=score<=59
case 0 ... 59:
cout<<"不及格"<<endl;
break;
//else
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
用if-else判斷
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
//宣告變數
//變數型態 變數名字
int score;
cout<<"請輸入你的成績:";
cin>>score;
//判斷成績
//90~100 -> 90<=score<=100
if(score>=90 && score<=100){
cout<<"優等"<<endl;
}
//80~89 -> 80<=score<=89
else if(score>=80 && score<=89){
cout<<"甲等"<<endl;
}
//70~79 -> 70<=score<=79
else if(score>=70 && score<=79){
cout<<"乙等"<<endl;
}
//60~69 -> 60<=score<=69
else if(score>=60 && score<=69){
cout<<"丙等"<<endl;
}
//0~59 -> 0<=score<=59
else if(score>=0 && score<=59){
cout<<"不及格"<<endl;
}
//0< score >100
else{
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
黃柏青
時間:
2022-4-20 21:13
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int score;
cout<<"請輸入您的成績: ";
cin>>score;
switch(score)
{
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 80 ... 89:
cout<<"甲等"<<endl;
break;
case 70 ... 79:
cout<<"乙等"<<endl;
break;
case 60 ... 69:
cout<<"丙等"<<endl;
break;
case 0 ... 59:
cout<<"不及格"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
黃裕恩
時間:
2022-4-20 21:13
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int score;
cout<<"請輸入你的成績:";
cin>>score;
switch(score){
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 80 ... 89:
cout<<"甲等"<<endl;
break;
case 70 ... 79:
cout<<"乙等"<<endl;
break;
case 60 ... 69:
cout<<"丙等"<<endl;
break;
case 0 ... 59:
cout<<"不及格"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
複製代碼
作者:
李彣
時間:
2022-4-20 21:13
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int score;
cout<<"請輸入你的成績:";
cin>>score;
switch(score)
{
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 80 ... 89:
cout<<"甲等"<<endl;
break;
case 70 ... 79:
cout<<"乙等"<<endl;
break;
case 60 ... 69:
cout<<"丙等"<<endl;
break;
case 0 ...
59:
cout<<"不及格"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
李睿宸
時間:
2022-4-20 21:14
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int score;
cout<<"請輸入你的成績:";
cin>>score;
switch(score)
{
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 80 ... 89:
cout<<"甲等"<<endl;
break;
case 70 ... 79:
cout<<"乙等"<<endl;
break;
case 60 ... 69:
cout<<"丙等"<<endl;
break;
case 0 ... 59:
cout<<"不及格"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
break;
}
system("pause");
return 0;
}
複製代碼
作者:
陳牧謙
時間:
2022-4-20 21:14
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int score ;
cout<<"請輸入你的成績 ";
cin>>score;
switch(score)
{
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 80 ... 89:
cout<<"甲等"<<endl;
break;
case 70 ... 79:
cout<<"乙等"<<endl;
break;
case 60 ... 69:
cout<<"丙等"<<endl;
case 0 ... 59:
cout<<"不及格"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
齊振睿
時間:
2022-4-20 21:15
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int score;
cout<<"請輸入您的成績: ";
cin>>score;
switch(score)
{
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 80 ... 89:
cout<<"甲等"<<endl;
break;
case 70 ... 79:
cout<<"乙等"<<endl;
break;
case 60 ... 69:
cout<<"丙等"<<endl;
break;
case 0 ... 59:
cout<<"不及格"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
鄭繼威
時間:
2022-4-27 20:21
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
//宣告變數
//變數型態 變數名字
int score;
cout<<"請輸入您的成績:";
cin>>score;
//if-else判斷選項
if(score==100) //在判斷兩邊的值是否相等,要用雙等號
cout<<"哇!滿分!"<<endl;
else if(score<100 && score>=60)
cout<<"恭喜你及格了,給你糖吃!"<<endl;
else if(score<60 && score>0)
cout<<"不及格!打屁股!"<<endl;
else if(score==0)
cout<<"零分?"<<endl;
else
cout<<"輸入錯誤!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
鄭繼威
時間:
2022-4-27 20:21
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
//宣告變數
//變數型態 變數名字
int score;
cout<<"請輸入您的成績:";
cin>>score;
//switch-case判斷選項
switch(score){
case 100:
cout<<"哇!滿分!"<<endl;
break;
case 60 ... 99:
cout<<"恭喜你及格了,給你糖吃!"<<endl;
break;
case 1 ... 59:
cout<<"不及格!打屁股!"<<endl;
break;
case 0:
cout<<"零分?"<<endl;
break;
//else
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
黃柏青
時間:
2022-5-4 20:21
1
複製代碼
作者:
林劭澧
時間:
2022-6-7 17:21
本帖最後由 鄭繼威 於 2022-6-13 19:38 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int score;
cout<<"你月考考幾分?:"<<endl;
cin>>score;
switch(score
{
case 90 ...100:
cout<<"優等"<<endl;
break;
case 89 ...80:
cout<<"甲等"<<endl;
break;
case 79 ...70:
cout<<"乙等"<<endl;
break;
case 69 ...60:
cout<<"丙等"<<endl;
break;
case 59 ...0:
cout<<"丁等"<<endl;
break;
default
cout<<"你再亂講,我就打死你"<<endl;
}
system("pause")
return 0;
}
複製代碼
因為太簡單了所以先偷吃步了
作者:
林劭澧
時間:
2022-6-13 20:06
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int score;
cout<<"你月考考幾分?:"<<endl;
cin>>score;
switch(score
{
case 90 ...100:
cout<<"優等"<<endl;
break;
case 89 ...80:
cout<<"甲等"<<endl;
break;
case 79 ...70:
cout<<"乙等"<<endl;
break;
case 69 ...60:
cout<<"丙等"<<endl;
break;
case 59 ...0:
cout<<"丁等"<<endl;
break;
default
cout<<"你再亂講,我就打死你"<<endl;
}
system("pause")
return 0;
}
複製代碼
作者:
林劭杰
時間:
2022-6-13 20:11
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int score;
cout<<"請輸入你的成績:";
cin>>score;
//判斷成績
switch(score){
case 90 ... 100:
cout<<"優等"<<endl;
break;
case 89 ... 80:
cout<<"甲等"<<endl;
break;
case 79 ... 70:
cout<<"乙等"<<endl;
break;
case 69 ... 60:
cout<<"丙等"<<endl;
break;
case 0 ... 59:
cout<<"不及格"<<endl;
break;
//else
default:
cout<<"你媽的"<<endl;
}
system("pause");
return 0;
}
複製代碼
好簡單
作者:
林劭杰
時間:
2022-6-20 19:49
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int day;
//case0~2
// -> 0<=score<=2
case0...2:
cout<<"有點少...加油!"<<endl;
//case3~4
// -> 3<=score<=4
case 3 ... 4:
cout<<"還不錯,繼續努力!"<<endl;
//case0~2
// -> 5<=score<=7
case 5...7:
cout<<"健康寶寶代言人"<<endl;
} system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2