我希望用户输入一个数字,但是如果它低于零我想显示一条错误信息,然后循环并询问用户另一个数字。这是我目前的代码。
// 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(" ");
}
。请帮助所有建议,我们将不胜感激!
由于
答案 0 :(得分:3)
我认为do-while循环在这里会更好,伪代码:
userInput = -1
do
{
userInput = Console.ReadLine
}
while (userInput <0)
Colin E。
答案 1 :(得分:1)
您走在正确的轨道上,只需查看关键字Continue
即可这是链接中的示例:
using System;
class ContinueTest
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i < 9)
{
continue;
}
Console.WriteLine(i);
}
}
}
注意:The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears.
答案 2 :(得分:0)
if (sales_figures[CustPos] < MIN_SALES_FIGURE) //True - Continue //FALSE -> Go back to start of loop
这里你实际上没有任何代码可以让它回到循环的开始。
我建议您首先将其全部写为伪代码,然后将其转换为代码:
if (number entered is too low)
then restart loop
otherwise carry on
答案 3 :(得分:0)
而不是if,你需要一段时间:
while( sales_figure[CustPos] < 0 )
{
Console.Write("enter sales figures for" + customer[CustPos] + " ");
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
}
这保证了在输入大于零的值之前它会一直保持提示。
继续,不执行您想要的操作。继续意味着“继续并忽略此迭代”,这意味着您的客户价值不正确。
答案 4 :(得分:0)
将WHILE循环与IF组合使用:
continueflag = 0;
while (continueflag == 0)
{
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
Console.WriteLine("");
if (sales_figures[CustPos] >= MIN_SALES_FIGURE) {
Console.WriteLine("entry invalid");
Console.WriteLine("enter another value");
} else continueflag = 1;
}