日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C 判断 —— switch语句(多个switch值与一组语句联系起来、case顺序是可任意的,default不一定是最后一个case)

發(fā)布時(shí)間:2025/10/17 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C 判断 —— switch语句(多个switch值与一组语句联系起来、case顺序是可任意的,default不一定是最后一个case) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一個 switch 語句允許測試一個變量等于多個值時(shí)的情況。每個值稱為一個 case,且被測試的變量會對每個 switch case 進(jìn)行檢查。

流程圖

//下面的語句是由用戶輸入的char變量值來控制 #include <stdio.h> int main(void) {char answer = 0;printf("Enter Y or N:");scanf(" %c",&answer);switch(answer){ //可以把多個case值與一組語句聯(lián)系起來case 'Y':case 'y':printf("You responded in the affirmative.\n");break;case 'N':case 'n':printf("You responded in the negative.\n");break;default:printf("You did not respond correctly...\n");break;}return 0; }

?/*
?* switch語句中的case順序是可任意的,default不一定是最后一個case
?* */

//輸入1-10內(nèi)的任意數(shù)字;各別數(shù)字對應(yīng)對應(yīng)不同的提示信息,有些數(shù)字沒有。 #include <stdio.h> int main() {int choice = 0;printf("Pick a number between 1 and 10 and you may win a prize:");scanf("%d",&choice);if((choice > 10) || (choice < 1))choice = 11;switch(choice){case 7:printf("You win the collected works of Amos of Amos Gruntfuttock.\n");break;case 2:printf("You win the folding thermomenter-pen-watch-unbrella.\n");break;case 8:printf("You win the lifetime supply of aspirin tablets.\n");break;case 11:printf("Try between 1 and 10.You wasted your guess.\n");default:printf("Sorry,you lose.\n");break;}return 0; }

?執(zhí)行結(jié)果顯示如下:

[root@J01051386 Test_20180418]# gcc switch.c [root@J01051386 Test_20180418]# ./a.out Pick a number between 1 and 10 and you may win a prize:3 Sorry,you lose. [root@J01051386 Test_20180418]# ./a.out Pick a number between 1 and 10 and you may win a prize:7 You win the collected works of Amos of Amos Gruntfuttock.

總結(jié)

以上是生活随笔為你收集整理的C 判断 —— switch语句(多个switch值与一组语句联系起来、case顺序是可任意的,default不一定是最后一个case)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。