c#程序设计如何用switch编写程序

来源:百度知道 编辑:UC知道 时间:2024/07/06 19:22:50
using System;
using System.Collections.Generic;
using System.Text;

namespace p2
{
class Program
{
static void Main(string[] args)
{
double x=Convert .ToDouble ( Console.ReadLine());
switch (x)
{
case x>0&&x<60 :
{
Console .WriteLine ("不及格,下次继续努力!");
}
break;
case x >= 60 && x < 70:
{
Console.WriteLine("及格");
}
break;
case x >= 70 && x < 90:
{
Console.WriteLine("良好");
}
break ;
case x<0||x>100:
{

Case后面跟的应该是x的某个特定值,而你的表达式返回的是一个bool类型,所以不匹配,如果涉及到取值范围,应该用if。。else来写,毕竟可以用switch的地方肯定可以用if,但是用if的地方就不一定能用switch了

就是说switch(x)
x要用整型 不能用double型

case 语句后面必须是和switch 括号里面类型对应的,而现在 case 语句后面的 是条件判断,结果是bool类型的
另外,siwtch里面应该是一个确定的值 而不应该是一个变量

switch里只能放整形数据(string,char,int 等)

case的值也不对,每一个case对应的值是唯一的。
要做这个判断用if可以。

前面的三个人说的对,第四个人说错了,如果switch里边是确定的值的话都没啥意义了,直接输出去求了!~~case后面你编写的是bool类型的,除非x是Bool类型的,并且switch后面的括号里只允许是整型的,下面是我改编后的...:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int x = Int32.Parse(Console.ReadLine().Trim());
switch (x/10)
{
case 10:
case 9:
{
Console.WriteLine("优秀");
}