请帮助我对下面的 if 语句感到非常困惑
我希望用户输入一个数字,但如果它低于零,我想显示一条错误消息,然后循环并询问用户另一个数字。这是我现在的代码。
// this determines what the loop does.
for (int CustPos = 0; CustPos < LineNum; CustPos = CustPos + 1)
{
// this asks the user to enter the sales figures
Console.Write("enter sales figures for" + customer[CustPos] + " ");
// this is user's input is read in and stored.
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
if (sales_figures[CustPos] < MIN_SALES_FIGURE) //True - continue
{
Console.WriteLine("");
Console.WriteLine("entry invalid");
Console.WriteLine("enter another value");
}
else//FALSE -> Go back to start of loop
{
Console.WriteLine("");
}
//this section displays the cust name, sales figure 70/30.
Console.WriteLine(" ");
fee_payable[CustPos] = (sales_figures[CustPos] / 100.0)
* licence_fee_in_percent[CustPos];
Console.WriteLine(customer[CustPos] +
" ----------- " + fee_payable[CustPos]);
Console.WriteLine("Licence fee to be paid in GBP is :" +
fee_payable[CustPos]);
seventy_percent_value = ((fee_payable[CustPos] / 10.0) * 7);
Console.WriteLine("70 percent of this fee is" +
seventy_percent_value);
thirty_percent_value = ((fee_payable[CustPos] / 10.0) * 3);
Console.WriteLine("30 percent of this fee is" +
thirty_percent_value);
Console.WriteLine(" ");
}
。请帮助所有建议,我们将不胜感激!
谢谢
I want the user to input a number, but if it is below zero I would like to show an error message and then loop round and ask the user for another number. Here is the code I have at the moment.
// this determines what the loop does.
for (int CustPos = 0; CustPos < LineNum; CustPos = CustPos + 1)
{
// this asks the user to enter the sales figures
Console.Write("enter sales figures for" + customer[CustPos] + " ");
// this is user's input is read in and stored.
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
if (sales_figures[CustPos] < MIN_SALES_FIGURE) //True - continue
{
Console.WriteLine("");
Console.WriteLine("entry invalid");
Console.WriteLine("enter another value");
}
else//FALSE -> Go back to start of loop
{
Console.WriteLine("");
}
//this section displays the cust name, sales figure 70/30.
Console.WriteLine(" ");
fee_payable[CustPos] = (sales_figures[CustPos] / 100.0)
* licence_fee_in_percent[CustPos];
Console.WriteLine(customer[CustPos] +
" ----------- " + fee_payable[CustPos]);
Console.WriteLine("Licence fee to be paid in GBP is :" +
fee_payable[CustPos]);
seventy_percent_value = ((fee_payable[CustPos] / 10.0) * 7);
Console.WriteLine("70 percent of this fee is" +
seventy_percent_value);
thirty_percent_value = ((fee_payable[CustPos] / 10.0) * 3);
Console.WriteLine("30 percent of this fee is" +
thirty_percent_value);
Console.WriteLine(" ");
}
. Please help all advice will be greatly appreciated!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 do-while 循环在这里会更好,伪代码:
Colin E.
I think a do-while loop would be better here, pseudocode:
Colin E.
您走在正确的道路上,只需查看关键字 继续
这是链接中的示例:
注意:
Continue 语句将控制传递给它出现的封闭迭代语句的下一个迭代。
Youre on the right track, just look at the keyword Continue
This is the example in the link:
Note:
The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears.
实际上,这里没有任何代码可以使其返回到循环的开头。
我建议您先将其全部写为伪代码,然后将其转换为代码:
You don't actually have any code here to make it go back to the start of the loop.
I recommend that you write it all out as pseudocode first, then turn it into code:
您需要的不是 if,而是 while:
这保证了它将不断提示,直到他们输入大于零的值。
继续,不做你想做的事。继续意味着“继续并忽略此迭代”,这意味着您对该客户的值不正确。
Instead of an if, you'll want a while:
Which guarantees that it will keep prompting until they enter something greater than zero.
Continue, does NOT do what you want it to. Continue means, "move on and ignore this iteration" which means you'd have an incorrect value for that customer.
将 WHILE 循环与 IF 结合使用:
Use a WHILE loop in combination with your IF: