C#checkBox Logic.NET
我试图写这个程序,给用户复选框,然后用户选择一个或两个,程序将返回相应的答案。但是程序不接受&&运算符。如果用户选中两个复选框,程序只返回第一个复选框的结果,而不是第二个复选框的结果。它甚至没有添加 代码如下:C#checkBox Logic.NET,c#,.net,C#,.net,我试图写这个程序,给用户复选框,然后用户选择一个或两个,程序将返回相应的答案。但是程序不接受&&运算符。如果用户选中两个复选框,程序只返回第一个复选框的结果,而不是第二个复选框的结果。它甚至没有添加 代码如下: private double Oillube() { //checking if the Oil is checked if (oilChbox.Checked) { return OIL_CHAN
private double Oillube()
{
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
else
{
//just returning to get ride off the red line under the Method
return 0;
}
}
需要首先执行
lubeChbox.Checked和&oilChbox.Checked
条件,因为这是两种条件中更具体的一种
如果lubeChbox.Checked
为真,或者oilChbox.Checked
为真,它将在到达lubeChbox.Checked&&oilChbox.Checked
条件之前返回,这意味着除非两者都为假,否则它不会达到条件
这就是为什么人们以这种方式使用return
通常是个坏主意,除非他们完全理解他们想要实现的代码流
以下是更正后的代码:
private double Oillube()
{
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//just returning to get ride off the red line under the Method
return 0;
}
或者,对于这种情况,您可以使用三级运算符,如下所示:
private double Oillube()
{
return (lubeChbox.Checked ? LUBE_JOB : 0) + (oilChbox.Checked ? OIL_CHANGE : 0);
}
第三级运算符的工作原理与语句相同,如果语句只能内联工作
因此:
(lubeChbox.Checked?润滑作业:0)
相当于此伪代码:
if(lubeChbox.Checked){LUBE_JOB}else{0}
需要先检查lubeChbox.Checked和&oilChbox.Checked
条件,因为这是两种条件中更具体的一种
如果lubeChbox.Checked
为真,或者oilChbox.Checked
为真,它将在到达lubeChbox.Checked&&oilChbox.Checked
条件之前返回,这意味着除非两者都为假,否则它不会达到条件
这就是为什么人们以这种方式使用return
通常是个坏主意,除非他们完全理解他们想要实现的代码流
以下是更正后的代码:
private double Oillube()
{
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//just returning to get ride off the red line under the Method
return 0;
}
或者,对于这种情况,您可以使用三级运算符,如下所示:
private double Oillube()
{
return (lubeChbox.Checked ? LUBE_JOB : 0) + (oilChbox.Checked ? OIL_CHANGE : 0);
}
第三级运算符的工作原理与语句相同,如果语句只能内联工作
因此:
(lubeChbox.Checked?润滑作业:0)
相当于此伪代码:
if(lubeChbox.Checked){LUBE_JOB}else{0}
由于AND条件比其他条件更具限制性,您应该首先检查它。像这样:
private double Oillube()
{
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//just returning to get ride off the red line under the Method
return 0;
}
由于AND条件比其他条件更具限制性,因此应首先检查它。像这样:
private double Oillube()
{
//if they are both checked
if(lubeChbox.Checked && oilChbox.Checked)
{
//Creating Variables sum to hold the value of the two
double sum;
sum = OIL_CHANGE + LUBE_JOB;
//returning sum
return sum;
}
//checking if the Oil is checked
if (oilChbox.Checked)
{
return OIL_CHANGE;
}
//checking if the lube is checked
if (lubeChbox.Checked)
{
return LUBE_JOB;
}
//just returning to get ride off the red line under the Method
return 0;
}
这是因为如果选中了第一个复选框,那么第一个if
语句为true,那么您将返回值,从而结束该方法
你应该替换你的if
语句,最后一个应该变为第一个,因为它是最具限制性的。这是因为如果选中了第一个复选框,那么第一个if
语句为true,那么你将返回值,从而结束该方法
你应该替换你的如果语句,最后一个应该成为第一个,因为它是最具限制性的