標題:
switch 判斷式 (三)
[打印本頁]
作者:
鄭繼威
時間:
2022-12-23 03:43
標題:
switch 判斷式 (三)
本帖最後由 鄭繼威 於 2022-12-23 03:45 編輯
利用 switch 判斷式,設計一
成績分級程式
,分級方式如下:
90分~100分 優等
80分~89分 甲等
70分~79分 乙等
60分~69分 丙等
0分~59分 不及格
不在以上範圍 輸入錯誤
利用 ... 代表一個範圍,就像<=
記得空格哦
ex:1到5
1<=x<=5 就是 1 ... 5
[使用者介面如下]
請輸入你的成績: 77
乙等!
請輸入你的成績: 101
輸入錯誤!
用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-12-23 20:54
#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-12-23 20:54
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int score;
cout<<"請輸入你的成績"<<endl;
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-12-23 20:56
#include<iostream>
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-12-23 20:56
#include<cstdlib>
using namespace std;
int main(){
int a;
cout<<"請輸入你的成績:";
cin>>a;
switch (a){
case 91 ... 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-12-23 21:00
#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-12-23 21:00
#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-12-23 21:01
#include <iostream>
using namespace std;
int main()
{
text:
int score;
cout<<"Enter your score: ";
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<<"oops, information out of reach...";
}
system ("pause");
goto text;
return 0;
}
複製代碼
作者:
呂宗晉
時間:
2022-12-23 21:04
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int score;
cout<<"請輸入你的成績:";
cin>>score;
if(score>=90 && score<=100){
cout<<"優等"<<endl;
}
else if(score>=80 && score<=89){
cout<<"甲等"<<endl;
else if(score>=70 && score<=79){
cout<<"乙等"<<endl;
}
else if(score>=60 && score<=69){
cout<<"丙等"<<endl;
}
else if(score>=0 && score<=59){
cout<<"不及格"<<endl;
}
else{
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
張桔熙
時間:
2022-12-30 18:25
#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
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2