开发者

Please help i have got very confused with the if statement below

开发者 https://www.devze.com 2023-01-31 03:22 出处:网络
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.

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//FALS开发者_StackOverflowE -> 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


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

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

Colin E.


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.


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消