新手求大神,有其他swit-case的思路写这个程序么?
生活随笔
收集整理的這篇文章主要介紹了
新手求大神,有其他swit-case的思路写这个程序么?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
兩個程序:
switch-case與if-else if的區別
相同點:可以實現多分支結構;
不同點:
switch:一般只能用于等值比較.(可以進行范圍運算???---學會用switch計算范圍出爐的思路____待解決)
if_else if:可以處理范圍計算.
?
switch(變量)
{
case 變量:
break;
}?
switch括號中的"變量"與case表達式中的"變量" 必須是同一類型,
或者是相兼容的數據類型.(一般是int類型或者string類型?).
?
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace 練習2 {class Program{static void Main(string[] args){//對成績進行考核評級Console.WriteLine("請輸入你的成績?");int score = Convert.ToInt32(Console.ReadLine()); switch (score/10){case 10:Console.WriteLine("A");break;case 9:Console.WriteLine("A");break;case 8:Console.WriteLine("B");break;case 7:Console.WriteLine("C");break;case 6:Console.WriteLine("D");break;default:Console.WriteLine("E");break;}Console.ReadKey();}} }總感覺這種方法有點繞思維,有可以直接判斷的么?難道只能用if-else if來寫么??? ↓↓↓
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace if_else_if寫switch作業 {class Program{static void Main(string[] args){Console.WriteLine("請輸入你的成績?");int score = Convert.ToInt32(Console.ReadLine());if (score >= 90){Console.WriteLine("A");}else if (score >= 80){Console.WriteLine("B");}else if (score >= 70){Console.WriteLine("C");}else if (score >= 60){Console.WriteLine("D");}else {Console.WriteLine("E");}Console.ReadKey();}} }?
轉載于:https://www.cnblogs.com/jerryho/p/3592808.html
總結
以上是生活随笔為你收集整理的新手求大神,有其他swit-case的思路写这个程序么?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Next.js 是怎么做预渲染的
- 下一篇: HDUOJ----4501小明系列故事—