有没有办法循环回到代码段的顶部? (这是一个非常糟糕的解释) [英] Is there a way to loop back to the top of a code segment? (This is a very bad explanation)

查看:65
本文介绍了有没有办法循环回到代码段的顶部? (这是一个非常糟糕的解释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在课堂上进行目前的练习,目标如下:

So I am working on my current exercise in my class and the objective is as follows:

"三位销售人员在Sunshine Hot Tubs-Andrea,Brittany和埃里克。编写一个基于控制台的应用程序,提示用户输入销售人员的初始('A','B'或'E')。虽然用户没有输入"Z",但继续提示销售人员进行的销售金额为
。将销售人员的佣金计算为销售额的10%,并将佣金添加到该销售人员的运行总额中。在用户键入"Z"作为首字母后,显示每个销售人员的总佣金。保存
文件作为TubSales.cs。"

"Three salespeople work at Sunshine Hot Tubs—Andrea, Brittany, and Eric. Write a console-based application that prompts the user for a salesperson’s initial (‘A’, ‘B’, or ‘E’). While the user does not type ‘Z’, continue by prompting for the amount of a sale the salesperson made. Calculate the salesperson’s commission as 10% of the sale amount, and add the commission to a running total for that salesperson. After the user types ‘Z’ for an initial, display each salesperson’s total commission earned. Save the file as TubSales.cs."

现在我知道我可以作弊并在网上复制和粘贴,并得到答案,但我想成为一个程序员在未来的职业生涯中所以我实际上是想让自己完成这项工作。

无论如何这里是我到目前为止所做的:

Now I know that I can cheat and literally copy and paste that online and get the answer, but I want to become a programmer in the future for a career so I am actually trying to make this work by myself.
Anyways here is what I have so far:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Dim and start
            double saleA = 0, saleB = 0, saleE = 0, commissionA = 0, totalA = 0, commissionB = 0, totalB = 0, commissionE = 0, totalE = 0;
            int done = 0, logoff = 0;
            Console.WriteLine("Give me your initial.....");

            // Initial Check
            string initial = Console.ReadLine();
            while (initial != "A" && initial != "B" && initial != "E")
            {
                Console.WriteLine("YOU'RE NOT DAVE!!!");
                initial = Console.ReadLine();
            }

            // Hello Initial
            if (initial == "A")
                Console.WriteLine("Hello Andrea!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "B")
                Console.WriteLine("Hello Brittany!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "E")
                Console.WriteLine("Hello Eric!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");



            // Calculate initial "A"'s values
            while (logoff == 0)
            {
                do
                {
                    do
                    {
                        saleA += Convert.ToDouble(Console.ReadLine());
                        commissionA = saleA * .10;
                        totalA = commissionA + saleA;
                        Console.WriteLine("Enter \"Z\" if you are finished, continue?");
                    } while (Console.ReadLine() != "Z" && initial == "A");

                    Console.WriteLine("Current Sales are: {0}", saleA);
                    done = 1;
                } while (initial == "A" && done == 0);

                logoff = 1;
            }
            

            // RESET VALUES
            done = 0;
            Console.WriteLine("Give me your initial.....");

            // Initial Check
            initial = Console.ReadLine();
            while (initial != "A" && initial != "B" && initial != "E")
            {
                Console.WriteLine("YOU'RE NOT DAVE!!!");
                initial = Console.ReadLine();
            }

            // Initial hello
            if (initial == "A")
                Console.WriteLine("Hello Andrea!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "B")
                Console.WriteLine("Hello Brittany!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "E")
                Console.WriteLine("Hello Eric!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");

            // Calculate initial "B"'s values
            while (Console.ReadLine() != "Z" && initial == "B")
            {
                saleB += Convert.ToDouble(Console.ReadLine());
                commissionB = saleB * .10;
                totalB = commissionB + saleB;
                Console.ReadLine();
            }

            // Initial Check
            initial = Console.ReadLine();
            while (initial != "A" && initial != "B" && initial != "E")
            {
                Console.WriteLine("YOU'RE NOT DAVE!!!");
                initial = Console.ReadLine();
            }

            // Inital hello
            if (initial == "A")
                Console.WriteLine("Hello Andrea!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "B")
                Console.WriteLine("Hello Brittany!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "E")
                Console.WriteLine("Hello Eric!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");

            // Calculate initial "E"'s values
            while (Console.ReadLine() != "Z" && initial == "E")
            {
                saleE += Convert.ToDouble(Console.ReadLine());
                commissionE = saleE * .10;
                totalE = commissionE + saleE;
                Console.ReadLine();
            }

            // Spacer
            Console.WriteLine();

            // Andrea stats
            Console.WriteLine("Sales Person Andrea has made a:" + Environment.NewLine + 
                "sale of: " + saleA.ToString("C2") + Environment.NewLine + 
                "commission of: " + commissionA.ToString("C2") + Environment.NewLine +
                "total of: " + totalA.ToString("C2"));

            // Brittany stats
            Console.WriteLine("Sales Person Andrea has made a:" + Environment.NewLine +
                "sale of: " + saleB.ToString("C2") + Environment.NewLine +
                "commission of: " + commissionB.ToString("C2") + Environment.NewLine +
                "total of: " + totalB.ToString("C2"));

            // Eric stats
            Console.WriteLine("Sales Person Andrea has made a:" + Environment.NewLine +
                "sale of: " + saleE.ToString("C2") + Environment.NewLine +
                "commission of: " + commissionE.ToString("C2") + Environment.NewLine +
                "total of: " + totalE.ToString("C2"));
        }
    }
}

但我现在关注的是"计算初始值"A"的值" ;在此代码块中:

But what I am focusing on for now is under "Calculate Initial "A"'s Values" in this code block:

            // Calculate initial "A"'s values
            while (logoff == 0)
            {
                do
                {
                    do
                    {
                        saleA += Convert.ToDouble(Console.ReadLine());
                        commissionA = saleA * .10;
                        totalA = commissionA + saleA;
                        Console.WriteLine("Enter \"Z\" if you are finished, continue?");
                    } while (Console.ReadLine() != "Z" && initial == "A");

                    Console.WriteLine("Current Sales are: {0}", saleA);
                    done = 1;
                } while (initial == "A" && done == 0);

                logoff = 1;
            }

因为你可以通过变量"logoff"的徒劳尝试看到,我希望它如此一旦所有的计算完成了"Andrea",它就会循环回来然后继续下去。

So as you can see by my vain attempt with the variable "logoff", I want it so that as soon as all the calculations have been completed for "Andrea", it loops back and then it continues down.

我这样做是为了如果这个人指定他正在编辑Andrea第一次,他可以输入"A"。再次编辑Andrea的值而不必选择编辑Brittany或Eric ....

I am doing this so that if the person specifies that he is editing Andrea the first time, he can type "A" again to edit Andrea's values instead of having to either select editing Brittany or Eric....

那么有没有办法像这样更有效地循环? (是的,因为我想成为一名程序员,所以我正在为这项任务做额外的工作)

So is there a way to loop back like that more effectively? (and yes I am being extra for this assignment as I usually do because I want to become a programmer)

推荐答案

您好,

在您的代码中,我可以看到很多复制粘贴。它创建了大量的代码和需求时间,了解所写的内容

In your code I can see a lot of copy-paste. Its create lot of code and demand time understand what was written

例如:

            // Initial hello
            if (initial == "A")
                Console.WriteLine("Hello Andrea!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "B")
                Console.WriteLine("Hello Brittany!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");
            else if (initial == "E")
                Console.WriteLine("Hello Eric!" + Environment.NewLine + "Insert the sales you have gained, and enter \"Z\" when you are finished....");

你应该这样做 

you supposed to do like 

SayHello(initial);

其中 

        private void SayHello(string initial)
        {
            // Hello Initial
            string name = InitialToName(initial);
            string message = string.Format("Hello {0}!", name); 
            Console.WriteLine(message);
            Console.WriteLine("Insert the sales you have gained, and enter \"Z\" when you are finished....");

        }

        private string InitialToName(string initial)
        { 
            string name = "Uncnown";

            if (initial == "A") name = "Andrea";
            if (initial == "B") name = "Brittany";
            if (initial == "E") name = "Eric";
            return name;
        }


这篇关于有没有办法循环回到代码段的顶部? (这是一个非常糟糕的解释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆