C # class introduction for loop

I am working on the following two issues for my C # level course. I performed problem 3, but I had problems with problem 4. The problem is that the total amount is not correct, because it does not add the first entered value of the combo, and I'm not quite sure where I made a mistake. I would appreciate any help you guys can provide with this. Please keep in mind that this is an intro level course, so it should be easy for loops if instructions are followed, statements made, etc. Here is the code that I still have:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter number of customers: ");
        var numCust = Convert.ToInt32(Console.ReadLine());
        int lunchCombo = 0;
        decimal total = 0;
        Console.WriteLine("Enter lunch combo purchased");
        lunchCombo = Convert.ToInt32(Console.ReadLine());
        for ( int i = 1; i < numCust; i++ )
            switch (lunchCombo)
            {
                case 1:
                    Console.WriteLine("Enter lunch combo purchased");
                    lunchCombo = Convert.ToInt32(Console.ReadLine());
                    total = total + 4.25M;
                    break;
                case 2:
                    Console.WriteLine("Enter lunch combo purchased");
                    lunchCombo = Convert.ToInt32(Console.ReadLine());
                    total = total + 5.75M;
                    break;
                case 3:
                    Console.WriteLine("Enter lunch combo purchased");
                    lunchCombo = Convert.ToInt32(Console.ReadLine());
                    total = total + 5.25M;
                    break;
                case 4:
                    Console.WriteLine("Enter lunch combo purchased");
                    lunchCombo = Convert.ToInt32(Console.ReadLine());
                    total = total + 3.75M;
                    break;
                default:
                    Console.WriteLine("Invalid input");
                    break;
            }
        Console.WriteLine("Your total is {0}", total);
        Console.ReadKey();
    }
}
  1. The restaurant has 4 dining combos for customers:

    Combo 1: Fried Chicken with Blind [price: 4.25] Combo 2: roast beef with mashed potatoes [price: 5.75] Combo 3: Fish and chips [price: 5.25] Combo 4: soup and salad [price: 3.75]

    , , . switch, , . " ", - . if... else.

  2. 3, , . . . - , . , , . , -1, . , .

+4
5

, switch. psuedo-, :

var total = 0;
var numCust = "How Many Customers?"

for (int i = 0; i < numCust; i++){
    var combo = "What Combo do you want?"
    switch (combo){
        case 1:
            total += 4.25;
            break;
        case 2:
            total += 5.25;
            break;
        case 4:
            total += 5.75;
            break;
    }
}

write("The total is: " + total);
0

, . , . , -, - .

loop (numCust) {
   read order number;
   loop (lunchCombo) {
      add to total;
   }
} 
0

for switch:

Console.WriteLine("Enter lunch combo purchased");
lunchCombo = Convert.ToInt32(Console.ReadLine());

, .

, for = 0 < numCust = 1 <= numCust. , .

0

for = 0 = 1. for switch

for (int i = 0; i < numCust; i++)
{
    Console.WriteLine("What is this customer order?");
    lunchCombo = Convert.ToInt32(Console.ReadLine());

    switch (lunchCombo)
    {
        case 1:
            total = total + 4.25M;
            break;
        case 2:
            total = total + 5.75M;
            break;
        case 3:
            total = total + 5.25M;
            break;
        case 4:
            total = total + 3.75M;
            break;
        default:
            Console.WriteLine("Invalid input");
            break;
    }
}
0

, 1 i.

That is, you start counting from 1 to n-1, which means that you loop one less time than you planned. So, if numCustomers is 4, the result of the loop:

i starts at | 0 | 1 | 2 | 3 | 4 |
---------------------------------
i loop...   | 4 | 3 | 2 | 1 | 0 |

So, I do not get enough time if I start with 1. You will need to make one of two changes. Change i to start at 0, or change the comparison as <=. Will work.

And don’t worry, this is a common mistake!

(Quantic credit to answer the comment!)

0
source

Source: https://habr.com/ru/post/1655658/


All Articles