-4

我正在尝试做一个程序,该程序接受三个首字母 A、B、C 之一的输入(让我们说三个家庭首字母),然后提示输入数字(销售额)。该程序需要不断要求另一个三个初始数字,然后再要求另一个数字。最后,当用户键入 Z 时,程序结束,但显示每个家庭的总销售额。

对于如何设置这样的程序,我有很多麻烦。

朝着正确的方向轻推会很棒。

http://postimg.org/image/9heb6c403/

如果你想看问题

是的,它的控制台底座。展示代码感觉很糟糕,但是

        string QUIT = "Z";
        string quit = "z";
        string purchaseString;
        string mainString;
        double salesA = 0;
        double salesB = 0;
        double salesC = 0;
        string andr1 = "a";
        string andr2 = "A";
        string bow1 = "b";
        string bow2 = "B";
        string clax1 = "c";
        string clax2 = "C";
        double purchase;

        Console.WriteLine("To begin press anykey. To see total enter Z or Z ");
        mainString = Console.ReadLine();
        while ((mainString == andr1 || mainString == andr2) || mainString != QUIT && mainString != quit)
        {
            Console.WriteLine("Enter the persons Intials: ");
            mainString = Console.ReadLine();
            if (mainString == andr1 || mainString == andr2)
            {
                Console.Write("Enter next purchase amount: ");
                purchaseString = Console.ReadLine();
                purchase = Convert.ToDouble(purchaseString);
                salesA += purchase;
            }
            if (mainString == bow1 || mainString == bow2)
            {
                Console.Write("Enter next purchase amount: ");
                purchaseString = Console.ReadLine();
                purchase = Convert.ToDouble(purchaseString);
                salesB += purchase;
            }
            if (mainString == clax1 || mainString == clax2)
            {
                Console.Write("Enter next purchase amount: ");
                purchaseString = Console.ReadLine();
                purchase = Convert.ToDouble(purchaseString);
                salesC += purchase;
            }
        }
        Console.WriteLine("Total for Anderson, Bowman and Claxton respectively is {0} {1} {2}", salesA.ToString("c"), salesB.ToString("c"), salesC.ToString("c"));
        Console.ReadLine();
    }
}

}

有没有另一种方法来启动它

Console.WriteLine("开始按任意键。要查看总数,请输入 Z 或 Z"); mainString = Console.ReadLine(); while ((mainString == andr1 || mainString == andr2) || mainString != QUIT && mainString != quit)

似乎没有必要

4

2 回答 2

0

Here are some nudges:

Console.ReadKey() reads a single character of input from the user.

After using string variableName = Console.ReadLine(); to get an input from a user as a string, you can then use a try-catch pair to see if it can be converted into a number or not.

You use while loops to keep repeating an action. At the end of each loop, set a conditional bool variable based on whether or not the user entered 'z' or whatever is the conditional for ending the program.

Let us know if you have some particular problem when debugging.

于 2013-10-21T00:09:11.053 回答
0

一些伪代码:

Set salesA, salesB, salesC = 0
While choice != 'Z'
Begin
    Initials = InputString
    Number = InputNumber
    If string[0] == 'A' Then salesA += Number
    Else If string[0] == 'B' Then salesB += Number
    Else If string[0] == 'C' Then salesC += Number
End
Print salesA, salesB and salesC
于 2013-10-21T00:04:26.610 回答