在c#中切换case语句 [英] switch case statement in c#
问题描述
大家好,
i有一个由控制台应用程序开发的给定程序的dout,请注意我的下面代码在该程序中我得到的输出是当我进入70它表明你是区别,50你失败。但我的问题是我只是想显示整个输入而不输入任何价值,
i需要给定的输出,
1.你是区别
2.average
3.failed
所以我在我的代码中更改了我在给定应用程序中初始化的内容,如果有任何解决方案,请告诉我..
我的代码是
使用系统;
使用 System.Collections.Generic;
使用 System.Text;
命名空间 ConsoleApplication2
{
class Program
{
静态 void Main( string [] args)
{
int mark;
Console.WriteLine( 输入标记);
mark = Convert.ToInt32(Console.ReadLine());
switch (mark)
{
case 70 :
Console.WriteLine( 你是区别< /跨度>);
Console.ReadLine();
break ;
case 60 :
Console.WriteLine( Average);
Console.ReadLine();
break ;
case 50 :
Console.WriteLine( failed);
Console.ReadLine();
break ;
}
}
}
}
问候,
stellus。
我不完全确定你要做什么,但如果你试图显示一系列的消息价值,
<前郎=文字> 70+区分
69 ... 60平均
59及以下失败然后一个开关是不是最好的方法:它只匹配确切的值,而不是范围。您必须列出交换机中的每个值:
switch (mark)
{
case 100 :
case 99 :
case 98 :
case 97 :
...
case 72 :
case 71 :
case 70 :
Console.WriteLine( 你是区别);
Console.ReadLine();
break ;哪个非常笨拙,难以阅读。
您可以使用开关,如果你使用了一个部门:
switch (( int )mark / 10 )
{
case 10 :
case 9 :
case 8 :
case 7 :
Console.WriteLine( 你是区别);
Console.ReadLine();
break ;
case 6 :
Console.WriteLine( Average);
Console.ReadLine();
break ;
...但即使这样,也难以维持 - 如果necxt年份平均分数变为65,那么该怎么办?
如果(标记 > = 70 )Console.WriteLine( 你是区别);
其他 如果(标记> = 60 )Console.WriteLine( Average 跨度>);
else Console.WriteLine( 失败 跨度>);
Console.Readline();
阅读起来要清楚得多,你可以用可读的常量或文件驱动来替换魔数值。
抱歉,让它成为一个开关?如果没有,这是'我的镜头:)
<前lang =c#> 代表 String GetEvaluationDelegate( int Mark_);
private static 列表< GetEvaluationDelegate> _Evaluations;
private static 列表< GetEvaluationDelegate>评估{
获取 {
如果(_评估== < span class =code-keyword> null ){
_Evaluations = new List< GetEvaluationDelegate>();
_Evaluations.Add( delegate ( int Mark_){返回( 70 < = Mark_? 你是区别: String .Empty);} );
_Evaluations.Add(委托( int Mark_){返回( 50 < = Mark_&& Mark_ < 70 ? average: String .Empty);});
_Evaluations.Add(委托( int Mark_){ return failed;}); // 默认
}
return _Evaluations;
}
}
静态 void Main( string [] args){
var mark = < span class =code-keyword> int .Parse(Console.ReadLine());
var evaluation = String .Empty;
var evaluateations = Evaluations.GetEnumerator();
while (评估== 字符串 .Empty&&评估。 MoveNext()){
evaluation = evaluations.Current(mark);
}
Console.WriteLine(评估);
Console.ReadLine();
}
...所以你可以在单独的列表中保留标记和评估。
尝试
class 程序
{
static void Main( string [] args)
{
int 标记;
Console.WriteLine( 你是区别);
Console.WriteLine( Average);
Console.WriteLine( failed);
Console.WriteLine( 输入标记);
mark = Convert.ToInt32(Console.ReadLine());
switch (mark)
{
case 70 :
Console.WriteLine( 你是区别< /跨度>);
Console.ReadLine();
break ;
case 60 :
Console.WriteLine( Average);
Console.ReadLine();
break ;
case 50 :
Console.WriteLine( failed);
Console.ReadLine();
break ;
}
}
Hi all,
i have a dout for given program which is developed by console application , the thing is please see my below code in that program i got output that is when i enter 70 it showing you are distinction ,and 50 you are failed.but my question is i just want to display the whole input without entering any value,
i need the given output,
1.you are distinction
2.average
3.failed
so what i change in my code or what i initialize in the given apps, please let me know if any solution about this..
my code is
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int mark;
Console.WriteLine("enter the mark");
mark = Convert.ToInt32(Console.ReadLine());
switch (mark)
{
case 70:
Console.WriteLine(" you are distinction");
Console.ReadLine();
break;
case 60:
Console.WriteLine("Average");
Console.ReadLine();
break;
case 50:
Console.WriteLine("failed");
Console.ReadLine();
break;
}
}
}
}
with regards,
stellus.
I''m not totally sure what you are trying to do, but if you are trying to show a message for a range of values,
70+ Distinction 69...60 Average 59 and below FailedThen a Switch is not the best way to do it: it only matches exact values, rather than a range. You woudl either have to list each value in the switch:
switch (mark) { case 100: case 99: case 98: case 97: ... case 72: case 71: case 70: Console.WriteLine(" you are distinction"); Console.ReadLine(); break;Which is very clumsy, and difficult to read.
You could use a switch, if you used a division:
switch ((int) mark / 10) { case 10: case 9: case 8: case 7: Console.WriteLine(" you are distinction"); Console.ReadLine(); break; case 6: Console.WriteLine(" Average"); Console.ReadLine(); break; ...But even then it is difficult to maintain - what if necxt year the Average mark is changed to 65?
You would be much better using an if...else construct:
if (mark >= 70) Console.WriteLine(" you are distinction"); else if (mark >= 60) Console.WriteLine(" Average"); else Console.WriteLine(" Failed"); Console.Readline();
It''s a lot clearer to read, and you can replace the "magic numbers" with readable constant, or file driven values.
Sorry, but have it to be a switch? If not, here''s my shot :)
delegate String GetEvaluationDelegate(int Mark_); private static List<GetEvaluationDelegate> _Evaluations; private static List<GetEvaluationDelegate> Evaluations { get { if (_Evaluations == null) { _Evaluations = new List<GetEvaluationDelegate>(); _Evaluations.Add(delegate(int Mark_) { return (70 <= Mark_ ? "you are distinction" : String.Empty); }); _Evaluations.Add(delegate(int Mark_) { return (50 <= Mark_ && Mark_ < 70 ? "average" : String.Empty); }); _Evaluations.Add(delegate(int Mark_) { return "failed"; }); // default } return _Evaluations; } } static void Main(string[] args) { var mark = int.Parse(Console.ReadLine()); var evaluation = String.Empty; var evaluations = Evaluations.GetEnumerator(); while (evaluation == String.Empty && evaluations.MoveNext()) { evaluation = evaluations.Current(mark); } Console.WriteLine(evaluation); Console.ReadLine(); }
... so you could maintain the marks and evaluations in a seperate list.
Try
class Program { static void Main(string[] args) { int mark; Console.WriteLine(" you are distinction"); Console.WriteLine("Average"); Console.WriteLine("failed"); Console.WriteLine("enter the mark"); mark = Convert.ToInt32(Console.ReadLine()); switch (mark) { case 70: Console.WriteLine(" you are distinction"); Console.ReadLine(); break; case 60: Console.WriteLine("Average"); Console.ReadLine(); break; case 50: Console.WriteLine("failed"); Console.ReadLine(); break; } }
这篇关于在c#中切换case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!