c#开关循环,为每种情况加总计

时间:2011-03-26 19:01:19

标签: c# loops while-loop switch-statement

我有2个代码,我想要合并为1,并且在执行此操作时遇到了很多麻烦。代码应该询问组号然后他们的捐赠金额并循环回到他们按0。一旦他们按0,它应该显示所有组的总数。以下是2个不同的代码

代码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"));
    }
}

代码2

using System;
namespace donate
{

class donate
{
    public static void Main()
    {


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




        }       
    }
}

所以基本上我想用第二个代码找到每个组的总数。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

不要像那样使用goto。只需像第一个代码段中那样使用另一个while循环。

说得更简洁:

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

而不是使用该switch语句。

基本上,您总结捐赠金额的代码可以解决这个问题,所以只需将其放在一个处理组号的类似循环中。如果你这样做,你会想要使用不同的输入来表示捐赠的结束与输入的结束。如果应用程序说“输入0退出输入”,然后“输入0退出捐赠输入”,那将会令人困惑。

答案 1 :(得分:0)

你与Code 2非常接近。如果你把Code 2放在while循环中它就可以了!

我在这里为你写的唯一代码是在while循环之外声明myint并将其初始化为非零值。

    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"));