Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#开关回路,将每种情况的总数相加_C#_Loops_While Loop_Switch Statement - Fatal编程技术网

c#开关回路,将每种情况的总数相加

c#开关回路,将每种情况的总数相加,c#,loops,while-loop,switch-statement,C#,Loops,While Loop,Switch Statement,我有两个代码,我想合并成1,我有很多麻烦做它。代码应该询问组号,然后询问他们的捐赠金额,并循环返回,直到他们按0。按0键后,应显示所有组的总数。这是两个不同的代码 代码1 using System; public class TotalPurchase { public static void Main() { double donation; double total = 0; string inputString;

我有两个代码,我想合并成1,我有很多麻烦做它。代码应该询问组号,然后询问他们的捐赠金额,并循环返回,直到他们按0。按0键后,应显示所有组的总数。这是两个不同的代码

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




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


任何帮助都将不胜感激。

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

更简洁的说法是:

if (myInt > 3 && myInt < 7) { ... }
if(myInt>3&&myInt<7){…}
而不是使用switch语句


基本上,你的代码用于汇总捐赠金额就可以了,所以只要把它放在一个类似的循环中就可以了,这个循环可以处理组号是多少。如果你这样做,你会想使用不同的输入来表示捐赠的结束,而不是输入的结束。如果应用程序说“输入0以退出输入”,然后说“输入0以退出捐赠输入”,这将令人困惑。

您与代码2非常接近。如果你把代码2放在循环中,它就会工作

我在这里为您编写的唯一代码是在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"));

这是一个学校的项目吗?