代码如下:
using System;
class TestSwtich
{
privatestatic void Main(string[] args)
{
const string myName ="karill";
const string sexName ="sexName";
const string sillName ="sillName";
string name;
Console.WriteLine("What's yourname?");
name = Console.ReadLine();
name = name.ToLower();
Console.WriteLine("inputname:{0}",name);
switch(name)
{
case myName:
Console.WriteLine("you have the same name asme!");
break;
case sexName:
Console.WriteLine("my,what a sexy name youhave!");
break;
case sillName:
Console.WriteLine("that 's a verysillName!");
& #160; break;
default:
break;
}
Console.WriteLine("hello:{0}",name);
Console.ReadKey();
}
}
在C/C++里swtich表达式是不能是字符类型的,只能是整型值的表达式.
代码如下:
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::cin;
int main(int argc, char *argv[])
{
stringstr;
cout<<"input integernumber:";
// scanf("%d",&a);
cin>>str;
switch(str){
case1:cout<<"Mondayn";
case2:cout<<"Tuesdayn";
case3:cout<<"Wednesdayn";
case4:cout<<"Thursdayn";
case5:cout<<"Fridayn";
case6:cout<<"Saturdayn";
case7:cout<<"Sundayn";
default:cout<<"errorn";
}
return0;
}