请帮助我对下面的 if 语句感到非常困惑

发布于 2024-10-08 14:31:32 字数 1509 浏览 0 评论 0原文

我希望用户输入一个数字,但如果它低于零,我想显示一条错误消息,然后循环并询问用户另一个数字。这是我现在的代码。

  // 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞎闹 2024-10-15 14:31:33

我认为 do-while 循环在这里会更好,伪代码:

userInput = -1
do
{
    userInput = Console.ReadLine
}
while (userInput <0)

Colin E.

I think a do-while loop would be better here, pseudocode:

userInput = -1
do
{
    userInput = Console.ReadLine
}
while (userInput <0)

Colin E.

夜吻♂芭芘 2024-10-15 14:31:33

您走在正确的道路上,只需查看关键字 继续

这是链接中的示例:

using System;
class ContinueTest 
{
    static void Main() 
    {
        for (int i = 1; i <= 10; i++) 
        {
            if (i < 9) 
            {
                continue;
            }
            Console.WriteLine(i);
        }
    }
}

注意:Continue 语句将控制传递给它出现的封闭迭代语句的下一个迭代。

Youre on the right track, just look at the keyword Continue

This is the example in the link:

using System;
class ContinueTest 
{
    static void Main() 
    {
        for (int i = 1; i <= 10; i++) 
        {
            if (i < 9) 
            {
                continue;
            }
            Console.WriteLine(i);
        }
    }
}

Note: The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears.

心在旅行 2024-10-15 14:31:33
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
if (sales_figures[CustPos] < MIN_SALES_FIGURE) //True - Continue                   //FALSE -> Go back to start of loop

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 (number entered is too low)
    then restart loop
    otherwise carry on
站稳脚跟 2024-10-15 14:31:33

您需要的不是 if,而是 while:

while( sales_figure[CustPos] < 0 )
{
    Console.Write("enter sales figures for" + customer[CustPos] + "  ");

    sales_figures[CustPos] = Double.Parse(Console.ReadLine());
}

这保证了它将不断提示,直到他们输入大于零的值。

继续,做你想做的事。继续意味着“继续并忽略此迭代”,这意味着您对该客户的值不正确。

Instead of an if, you'll want a while:

while( sales_figure[CustPos] < 0 )
{
    Console.Write("enter sales figures for" + customer[CustPos] + "  ");

    sales_figures[CustPos] = Double.Parse(Console.ReadLine());
}

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.

遮云壑 2024-10-15 14:31:33

将 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;
}

Use a WHILE loop in combination with your 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文