开发者

c# switch loop, adding up total for each case

开发者 https://www.devze.com 2023-02-20 04:27 出处:网络
I have 2 codes that I want to combine into 1 and am having a lot of trouble doing it. The code should ask for the group number then their donation amount and loop back until they press 0. Once they pr

I have 2 codes that I want to combine into 1 and am having a lot of trouble doing it. The code should ask for the group number then their donation amount and loop back until they press 0. Once they press 0 it should show the total for all groups. Here are the 2 different codes

code 1

using System;
public class TotalPurchase
{
    public static void Main()
    {
        double donation;
        double total = 0;
        string inputString;
        const double QUIT = 0;
        Console.WriteLine("Please enter the amount of the contribution: ");
        inputString = Console.ReadLine();
        donation = Convert.ToDouble(inputString);
        while(donation != QUIT)
        {
            total += donation;
            Console.WriteLine("Enter next donation amount, or " +
                QUIT + " to quit ");
            inputString = Console.ReadLine();
            donation = Convert.ToDouble(inputString);
        }
        Console.WriteLine("Your total is {0}", total.ToString("C"));
    }
}

code 2

using System;
namespace donate
{

class donate
{
    public static void Ma开发者_C百科in()
    {


        begin:
        string group;
        int myint;
        Console.WriteLine("Please enter group number (4, 5, or 6)");
        Console.WriteLine("(0 to quit): ");
        group = Console.ReadLine();
        myint = Int32.Parse(group);

            switch (myint)
            {
                case 0:
                    Console.WriteLine("Bye.");
                    break;
                case 4:
                case 5:
                case 6:
                     double donation;

                     string inputString;

                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString = Console.ReadLine();
                    donation = Convert.ToDouble(inputString);
                    goto begin;
                default:
                    Console.WriteLine("Incorrect grade number.", myint);
                    goto begin;
                }




        }       
    }
}

So basically I want to find the total for each group using the 2nd code.

Any help would be greatly appreciated.


Don't use goto like that. Just use another while loop like you did in the first code segement.

It would be much more concise to say:

if (myInt > 3 && myInt < 7) { ... }

instead of using that switch statement.

Basically your code for summing up the donation amount does the trick, so just stick that inside a similar loop that handles what the group number is. If you do this you're going to want to use a different input to signify the end of donations vs the end of input altogether. It would be confusing if the application said "Enter 0 to quit input", followed by "Enter 0 to quit donation input".


You are very close with Code 2. If you put Code 2 inside while loop it works!

The only code I have written for you here is declared myint outside the while loop and initialised it to a non-zero value.

    double total = 0;
    int myint = -1;

    while (myint != 0)
    {
        string group;

        Console.WriteLine("Please enter group number (4, 5, or 6)");
        Console.WriteLine("(0 to quit): ");
        group = Console.ReadLine();
        myint = Int32.Parse(group);

        switch (myint)
        {
            case 0:
                Console.WriteLine("Bye.");
                break;
            case 4:
            case 5:
            case 6:
                double donation;
                string inputString;
                Console.WriteLine("Please enter the amount of the contribution: ");
                inputString = Console.ReadLine();
                donation = Convert.ToDouble(inputString);
                total += donation;
                break;
            default:
                Console.WriteLine("Incorrect grade number.", myint);
                break;
        }
    }

    Console.WriteLine("Your total is {0}", total.ToString("C"));
0

精彩评论

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

关注公众号