|M| 谁帮我简写这一段string代码
string ticketCount1; //成人票数量
string ticketCount2; //成人票数量
string ticketCount3; //成人票数量
string ticketCount4; //成人票数量
string ticketCount5; //成人票数量
if (dr[ "TicketCount "].ToString() != "1 ")
{
ticketCount1 = "NFILL ";
}
else
{
ticketCount1 = "FILL ";
}
if (dr[ "TicketCount "].ToString() != "2 ")
{
ticketCount2 = "NFILL ";
}
else
{
ticketCount2 = "FILL ";
}
if (dr[ "TicketCount "].ToString() != "3 ")
{
ticketCount3 = "NFILL ";
}
else
{
ticketCount3 = "FILL ";
}
if (dr[ "TicketCount "].ToString() != "4 ")
{
ticketCount4 = "NFILL ";
}
else
{
ticketCount4 = "FILL ";
}
if (dr[ "TicketCount "].ToString() != "5 ")
{
ticketCount5 = "NFILL ";
}
else
{
ticketCount5 = "FILL ";
}
谢谢
这样写太长了有没有短的,最好用数组
[解决办法]
string[] ticketCount = new string[5];//成人票数量
for (int i = 0; i < 5; i++)
{
if (dr[ "TicketCount "].ToString() != (i + 1).ToString())
{
ticketCount[i] = "NFILL ";
}
else
{
ticketCount[i] = "FILL ";
}
}
[解决办法]
string ticketCount1 = "NFILL "; //成人票数量
string ticketCount2 = "NFILL "; //成人票数量
string ticketCount3 = "NFILL "; //成人票数量
string ticketCount4 = "NFILL "; //成人票数量
string ticketCount5 = "NFILL "; //成人票数量
switch(dr[ "TicketCount "].ToString())
{
case "1 ":
ticketCount1 = "FILL ";
break;
case "2 ":
ticketCount2 = "FILL ";
break;
case "3 ":
ticketCount3 = "FILL ";
break;
case "4 ":
ticketCount4 = "FILL ";
break;
case "5 ":
ticketCount5 = "FILL ";
break;
}
[解决办法]
string[] ticketCount=new string[6]; //成人票数量
for(int i=1;i <=5;i++){
if(dr[ "TicketCount "].ToString() !=i.ToString()){
ticketCount[i] = "NFILL ";
}
else{
ticketCount[i]= "FILL ";
}
}
[解决办法]
用switch case语句.
[解决办法]
string ticketCount1 = "NFILL "; //成人票数量
string ticketCount2 = "NFILL "; //成人票数量
string ticketCount3 = "NFILL "; //成人票数量
string ticketCount4 = "NFILL "; //成人票数量
string ticketCount5 = "NFILL "; //成人票数量
switch(dr[ "TicketCount "].ToString())
{
case "1 ":
ticketCount1 = "FILL ";
break;
case "2 ":
ticketCount2 = "FILL ";
break;
case "3 ":
ticketCount3 = "FILL ";
break;
case "4 ":
ticketCount4 = "FILL ";
break;
case "5 ":
ticketCount5 = "FILL ";
break;
}
---------------------------------
同意!
case 比if快,结构也清晰
[解决办法]
楼上正解
[解决办法]
这个 看编译器。。。
[解决办法]
呵呵,已有解,顶一下
[解决办法]
string ticketCount1 = "NFILL "; //成人票数量
string ticketCount2 = "NFILL "; //成人票数量
string ticketCount3 = "NFILL "; //成人票数量
string ticketCount4 = "NFILL "; //成人票数量
string ticketCount5 = "NFILL "; //成人票数量
switch(dr[ "TicketCount "].ToString())
{
case "1 ":
ticketCount1 = "FILL ";
break;
case "2 ":
ticketCount2 = "FILL ";
break;
case "3 ":
ticketCount3 = "FILL ";
break;
case "4 ":
ticketCount4 = "FILL ";
break;
case "5 ":
ticketCount5 = "FILL ";
break;
}
---------------------------------
同意!
最优方案