標題:
if...else if...else 判斷式 (二)
[打印本頁]
作者:
鄭又綸
時間:
2025-5-24 08:46
標題:
if...else if...else 判斷式 (二)
利用
if...else if...else
語法,試做一成績分級程式。
90分以上 100分以下 優
80分以上 未達90分 甲
70分以上 未達80分 乙
60分以上 未達70分 丙
0分以上 未達60分 丁
不在上述範圍 輸入錯誤
&& and
|| or
方法一:以 && 精準涵蓋判斷範圍
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
float score;
re:
cout<<"請輸入你的成績: ";
cin>>score;
if(score<=100 && score>=90)
cout<<"優"<<endl;
else if(score<90 && score>=80)
cout<<"甲"<<endl;
else if(score<80 && score>=70)
cout<<"乙"<<endl;
else if(score<70 && score>=60)
cout<<"丙"<<endl;
else if(score<60 && score>=0)
cout<<"丁"<<endl;
else
cout<<"輸入錯誤!"<<endl;
cout<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
方法二:藉由程式碼由上而下讀取的特性,只判斷單一方向。
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
float score;
re:
cout<<"請輸入你的成績: ";
cin>>score;
if(score>100)
cout<<"輸入錯誤!"<<endl;
else if(score>=90)
cout<<"優"<<endl;
else if(score>=80)
cout<<"甲"<<endl;
else if(score>=70)
cout<<"乙"<<endl;
else if(score>=60)
cout<<"丙"<<endl;
else if(score>=0)
cout<<"丁"<<endl;
else
cout<<"輸入錯誤!"<<endl;
cout<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
黃品錡
時間:
2025-5-28 20:21
此帖僅作者可見
作者:
黃柏穎
時間:
2025-5-28 20:22
此帖僅作者可見
作者:
王崇睿
時間:
2025-5-28 20:23
此帖僅作者可見
作者:
李承叡
時間:
2025-5-28 20:23
此帖僅作者可見
作者:
郭昊恩
時間:
2025-5-28 20:26
此帖僅作者可見
作者:
蔡炘成
時間:
2025-5-28 20:27
此帖僅作者可見
作者:
吳睿恩
時間:
2025-5-28 20:31
此帖僅作者可見
作者:
何希衡
時間:
2025-5-28 20:41
此帖僅作者可見
作者:
陳沛錞
時間:
5 天前 20:28
此帖僅作者可見
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2