控制台应用程序 C# 循环

发布于 2024-12-11 08:44:34 字数 4168 浏览 0 评论 0原文

您好,我正在学习成为一名程序员,这是我正在从事的一个项目。控制台基本上接受用户输入(直径)并告诉您将获得多少个披萨片以及每片的面积。在告诉他们每个切片和面积后,它必须提示新的输入。切片是根据直径范围确定的,例如 12 - 20" 直径 = 8 个切片。用户必须输入 12" - 36" 之间的范围,否则会收到错误消息。此外,如果用户输入直径 36,它必须以 4 的减量显示所有其他切片,因此 24 , 20 , 16 ,12 , 8 ,但如果直径最终落在 8 以下。切片,因此 12 切片将显示 8 切片和 12 切片

最后,我们正在学习循环,我们将使用 do while 循环,

这就是我所拥有的 for 循环。但它不起作用

        //DECLARATIONS
        double circleArea;  // area of pizza
        double diameter; // diameter of the pizza
        double areaOfSlice; // area of the slices
        double radius; // half the diameter of the pizza
        double slices = 0; // number of pizza slices
        string message = ""; // a string to hold a message to the user
        const int DIAMETER_RANGE_MASSIVE = 36;
        const int DIAMETER_RANGE_EXTRA_LARGE = 30;
        const int DIAMETER_RANGE_LARGE = 24;
        const int DIAMETER_RANGE_MED = 16;
        const int DIAMETER_RANGE_LOW = 12; // Low end on range scale for diameter
        const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
        const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
        const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
        const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter        
        bool needInput = true;
        const int END_PROGRAM = 0;




        // INPUT
        // Prompt for and get keyboard input

        Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
        diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,
        // convert that string to an double,
        // assign the resulting value to diameter

        // PROCESSING
        // determine if diameter meets requirements of 12" to 36"
        // if does not meet requirements show error message and have user enter in new diameter
        do
        {
            if (diameter < DIAMETER_RANGE_LOW || diameter > DIAMETER_RANGE_MASSIVE)

            {
                message = "\nENTRY ERROR";
                message += "\nPizza must have a diameter in the range of 12” to 36” inclusive!";
                message += "\nPlease try again.";
            }



        else {    
            needInput = false;
                // determine the number of slices based on diameter


                if (diameter >= DIAMETER_RANGE_LOW && diameter < DIAMETER_RANGE_MED)
                {
                    slices = (SLICES_LOW_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_LARGE)
                {
                    slices = (SLICES_MID_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
                {
                    slices = (SLICES_HIGH_DIAMETER);
                }
                else
                {
                    slices = (SLICES_GIANT_DIAMETER);
                }

                Console.Clear(); // clears console to show new output lines
                //OUTPUT
                for (int slicesAddFour = 8; slicesAddFour <=slices; slicesAddFour+=4) // for each slices
                {
                // determine the area of the slices
                radius = diameter / 2; // uses diameter to get radius
                circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area  
                areaOfSlice = Math.Round((circleArea / slices), 2); // divides circle area by slices, takes the result of above calculation and rounds

                Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter); 
                message +=("\n==============================================");
                Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.",areaOfSlice,slices);

                }


        } //end of else

            message = ("\nPlease enter the diameter of your pizza (0 to end program)");
            needInput = true;
        } while (diameter != END_PROGRAM && needInput);
    }
}

Hi i am studing to be a programmer and this is a project i am working on. The console basically takes the users input (diameter) and tells you how many pizza slices you will get and the area of each slice. after telling them the slices and area of each, it must promt for new input. Slices are determined based on ranges such as 12 - 20" of diameter = 8 slices. a user has to enter in a range between 12" - 36" or they get an error message. Also if the user enters in a diameter of 36, it must show all of the other slices in decrements of 4, so 24 , 20, 16 ,12 , 8 . but if the diameter ends up falling under the 8 slices, it must only show that. hence 12 slices will show 8 slices and 12 slices.

lastly, we are learning about loops, we are to use the do while loop, and the for loop in this project.

this is what i have, and it doesnt work.

        //DECLARATIONS
        double circleArea;  // area of pizza
        double diameter; // diameter of the pizza
        double areaOfSlice; // area of the slices
        double radius; // half the diameter of the pizza
        double slices = 0; // number of pizza slices
        string message = ""; // a string to hold a message to the user
        const int DIAMETER_RANGE_MASSIVE = 36;
        const int DIAMETER_RANGE_EXTRA_LARGE = 30;
        const int DIAMETER_RANGE_LARGE = 24;
        const int DIAMETER_RANGE_MED = 16;
        const int DIAMETER_RANGE_LOW = 12; // Low end on range scale for diameter
        const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
        const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
        const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
        const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter        
        bool needInput = true;
        const int END_PROGRAM = 0;




        // INPUT
        // Prompt for and get keyboard input

        Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
        diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,
        // convert that string to an double,
        // assign the resulting value to diameter

        // PROCESSING
        // determine if diameter meets requirements of 12" to 36"
        // if does not meet requirements show error message and have user enter in new diameter
        do
        {
            if (diameter < DIAMETER_RANGE_LOW || diameter > DIAMETER_RANGE_MASSIVE)

            {
                message = "\nENTRY ERROR";
                message += "\nPizza must have a diameter in the range of 12” to 36” inclusive!";
                message += "\nPlease try again.";
            }



        else {    
            needInput = false;
                // determine the number of slices based on diameter


                if (diameter >= DIAMETER_RANGE_LOW && diameter < DIAMETER_RANGE_MED)
                {
                    slices = (SLICES_LOW_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_LARGE)
                {
                    slices = (SLICES_MID_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
                {
                    slices = (SLICES_HIGH_DIAMETER);
                }
                else
                {
                    slices = (SLICES_GIANT_DIAMETER);
                }

                Console.Clear(); // clears console to show new output lines
                //OUTPUT
                for (int slicesAddFour = 8; slicesAddFour <=slices; slicesAddFour+=4) // for each slices
                {
                // determine the area of the slices
                radius = diameter / 2; // uses diameter to get radius
                circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area  
                areaOfSlice = Math.Round((circleArea / slices), 2); // divides circle area by slices, takes the result of above calculation and rounds

                Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter); 
                message +=("\n==============================================");
                Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.",areaOfSlice,slices);

                }


        } //end of else

            message = ("\nPlease enter the diameter of your pizza (0 to end program)");
            needInput = true;
        } while (diameter != END_PROGRAM && needInput);
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏见 2024-12-18 08:44:34

您的问题是您读入一次数据,然后继续循环而没有获取任何新数据。如果您移动到

Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

循环内部,它应该可以解决您的问题

Your problem is that you read in the data once, and then continue looping without getting any new data. If you move

Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

inside of the loop, it should fix your problem

卸妝后依然美 2024-12-18 08:44:34

移动这两行:

Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
            diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

在其中的 do/while 循环内部。

你的代码变成:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOHelp
{
    class Program
    {
        static void Main(string[] args)
        {
              //DECLARATIONS
        double circleArea;  // area of pizza
        double diameter; // diameter of the pizza
        double areaOfSlice; // area of the slices
        double radius; // half the diameter of the pizza
        double slices = 0; // number of pizza slices
        string message = ""; // a string to hold a message to the user
        const int DIAMETER_RANGE_MASSIVE = 36;
        const int DIAMETER_RANGE_EXTRA_LARGE = 30;
        const int DIAMETER_RANGE_LARGE = 24;
        const int DIAMETER_RANGE_MED = 16;
        const int DIAMETER_RANGE_LOW = 12; // Low end on range scale for diameter
        const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
        const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
        const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
        const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter        
        bool needInput = true;
        const int END_PROGRAM = 0;




        // INPUT
        // Prompt for and get keyboard input


        // convert that string to an double,
        // assign the resulting value to diameter

        // PROCESSING
        // determine if diameter meets requirements of 12" to 36"
        // if does not meet requirements show error message and have user enter in new diameter
        string message = "Please enter the diameter of your pizza (0 to end program): ";

        do
        {
            Console.Write(message);  // get user to input diameter
            diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

            if (diameter < DIAMETER_RANGE_LOW || diameter > DIAMETER_RANGE_MASSIVE)

            {
                message = "\nENTRY ERROR";
                message += "\nPizza must have a diameter in the range of 12” to 36” inclusive!";
                message += "\nPlease try again.";
            }



        else {    
            needInput = false;
                // determine the number of slices based on diameter


                if (diameter >= DIAMETER_RANGE_LOW && diameter < DIAMETER_RANGE_MED)
                {
                    slices = (SLICES_LOW_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_LARGE)
                {
                    slices = (SLICES_MID_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
                {
                    slices = (SLICES_HIGH_DIAMETER);
                }
                else
                {
                    slices = (SLICES_GIANT_DIAMETER);
                }

                Console.Clear(); // clears console to show new output lines
                //OUTPUT
                for (int slicesAddFour = 8; slicesAddFour <=slices; slicesAddFour+=4) // for each slices
                {
                // determine the area of the slices
                radius = diameter / 2; // uses diameter to get radius
                circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area  
                areaOfSlice = Math.Round((circleArea / slices), 2); // divides circle area by slices, takes the result of above calculation and rounds

                Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter); 
                message +=("\n==============================================");
                Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.",areaOfSlice,slices);

                }


        } //end of else

            message = ("\nPlease enter the diameter of your pizza (0 to end program)");
            needInput = true;
        } while (diameter != END_PROGRAM && needInput);
    }
}
        }

Move these two lines:

Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
            diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

Inside of the do/while loop within it.

Your code becomes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SOHelp
{
    class Program
    {
        static void Main(string[] args)
        {
              //DECLARATIONS
        double circleArea;  // area of pizza
        double diameter; // diameter of the pizza
        double areaOfSlice; // area of the slices
        double radius; // half the diameter of the pizza
        double slices = 0; // number of pizza slices
        string message = ""; // a string to hold a message to the user
        const int DIAMETER_RANGE_MASSIVE = 36;
        const int DIAMETER_RANGE_EXTRA_LARGE = 30;
        const int DIAMETER_RANGE_LARGE = 24;
        const int DIAMETER_RANGE_MED = 16;
        const int DIAMETER_RANGE_LOW = 12; // Low end on range scale for diameter
        const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
        const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
        const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
        const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter        
        bool needInput = true;
        const int END_PROGRAM = 0;




        // INPUT
        // Prompt for and get keyboard input


        // convert that string to an double,
        // assign the resulting value to diameter

        // PROCESSING
        // determine if diameter meets requirements of 12" to 36"
        // if does not meet requirements show error message and have user enter in new diameter
        string message = "Please enter the diameter of your pizza (0 to end program): ";

        do
        {
            Console.Write(message);  // get user to input diameter
            diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

            if (diameter < DIAMETER_RANGE_LOW || diameter > DIAMETER_RANGE_MASSIVE)

            {
                message = "\nENTRY ERROR";
                message += "\nPizza must have a diameter in the range of 12” to 36” inclusive!";
                message += "\nPlease try again.";
            }



        else {    
            needInput = false;
                // determine the number of slices based on diameter


                if (diameter >= DIAMETER_RANGE_LOW && diameter < DIAMETER_RANGE_MED)
                {
                    slices = (SLICES_LOW_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_LARGE)
                {
                    slices = (SLICES_MID_DIAMETER);
                }
                else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
                {
                    slices = (SLICES_HIGH_DIAMETER);
                }
                else
                {
                    slices = (SLICES_GIANT_DIAMETER);
                }

                Console.Clear(); // clears console to show new output lines
                //OUTPUT
                for (int slicesAddFour = 8; slicesAddFour <=slices; slicesAddFour+=4) // for each slices
                {
                // determine the area of the slices
                radius = diameter / 2; // uses diameter to get radius
                circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area  
                areaOfSlice = Math.Round((circleArea / slices), 2); // divides circle area by slices, takes the result of above calculation and rounds

                Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter); 
                message +=("\n==============================================");
                Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.",areaOfSlice,slices);

                }


        } //end of else

            message = ("\nPlease enter the diameter of your pizza (0 to end program)");
            needInput = true;
        } while (diameter != END_PROGRAM && needInput);
    }
}
        }
荆棘i 2024-12-18 08:44:34
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab6B
{
    class Program
    {
        static void Main(string[] args)
        {
            //DECLARATIONS
            double circleArea;  // area of pizza
            double diameter; // diameter of the pizza
            double areaOfSlice; // area of the slices
            double radius; // half the diameter of the pizza
            double slices = 0; // number of pizza slices
            const int INPUT_MAX = 36; // maximum entry for diameter
            const int DIAMETER_RANGE_EXTRA_LARGE = 30; // diameter range used to figure out slices
            const int DIAMETER_RANGE_LARGE = 24; // diameter range used to figure out slices
            const int DIAMETER_RANGE_MED = 16; // diameter range used to figure out slices
            const int INPUT_MIN = 12; // minimum entry for diameter
            const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
            const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
            const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
            const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter        
            bool needInput = true; // does the program need input true or false
            const int END_PROGRAM = 0; // 0 ends the program





            // INPUT
            // Prompt for and get keyboard input

            Console.Write("Please enter the diameter of your pizza: ");  // get user to input diameter
            diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard, 

            // convert that string to an double,
            // assign the resulting value to diameter

            while (diameter != END_PROGRAM && needInput) // Begin while loop

            {

                // PROCESSING

                // determine if diameter meets requirements of 12" to 36"
                // if does not meet requirements show error message and have user enter in new diameter


                if (diameter < INPUT_MIN || diameter > INPUT_MAX)
                {

                    Console.WriteLine( "\nENTRY ERROR\nPizza must have a diameter in the range of 12” to 36” inclusive!\nPlease try again.");
                }


                else
                {
                    needInput = false;
                    // determine the number of slices based on diameter


                    if (diameter < DIAMETER_RANGE_MED)
                    {
                        slices = (SLICES_LOW_DIAMETER);
                    }
                    else if (diameter < DIAMETER_RANGE_LARGE)
                    {
                        slices = (SLICES_MID_DIAMETER);
                    }
                    else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
                    {
                        slices = (SLICES_HIGH_DIAMETER);
                    }
                    else
                    {
                        slices = (SLICES_GIANT_DIAMETER);
                    }

                    Console.Clear(); // clears console to show new output lines

                    //OUTPUT

                    Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter);
                    Console.WriteLine("\n==============================================");

                    for (int slicesAddFour = 8; slicesAddFour <= slices; slicesAddFour += 4) // for each slices
                    {
                        // determine the area of the slices
                        radius = diameter / 2; // uses diameter to get radius
                        circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area  
                        areaOfSlice = Math.Round((circleArea / slicesAddFour), 2); // divides circle area by slices, takes the result of above calculation and rounds


                        Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.", slicesAddFour, areaOfSlice);


                    }


                } //end of else

                Console.Write("\nPlease enter the diameter of your pizza (0 to exit): ");
                diameter = Convert.ToDouble(Console.ReadLine());
                Console.Clear(); // clears console to show new output lines

                // set the need input value back to true for new entry
                needInput = true;

             } // end of while loop
        }
    }
}
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab6B
{
    class Program
    {
        static void Main(string[] args)
        {
            //DECLARATIONS
            double circleArea;  // area of pizza
            double diameter; // diameter of the pizza
            double areaOfSlice; // area of the slices
            double radius; // half the diameter of the pizza
            double slices = 0; // number of pizza slices
            const int INPUT_MAX = 36; // maximum entry for diameter
            const int DIAMETER_RANGE_EXTRA_LARGE = 30; // diameter range used to figure out slices
            const int DIAMETER_RANGE_LARGE = 24; // diameter range used to figure out slices
            const int DIAMETER_RANGE_MED = 16; // diameter range used to figure out slices
            const int INPUT_MIN = 12; // minimum entry for diameter
            const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
            const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
            const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
            const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter        
            bool needInput = true; // does the program need input true or false
            const int END_PROGRAM = 0; // 0 ends the program





            // INPUT
            // Prompt for and get keyboard input

            Console.Write("Please enter the diameter of your pizza: ");  // get user to input diameter
            diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard, 

            // convert that string to an double,
            // assign the resulting value to diameter

            while (diameter != END_PROGRAM && needInput) // Begin while loop

            {

                // PROCESSING

                // determine if diameter meets requirements of 12" to 36"
                // if does not meet requirements show error message and have user enter in new diameter


                if (diameter < INPUT_MIN || diameter > INPUT_MAX)
                {

                    Console.WriteLine( "\nENTRY ERROR\nPizza must have a diameter in the range of 12” to 36” inclusive!\nPlease try again.");
                }


                else
                {
                    needInput = false;
                    // determine the number of slices based on diameter


                    if (diameter < DIAMETER_RANGE_MED)
                    {
                        slices = (SLICES_LOW_DIAMETER);
                    }
                    else if (diameter < DIAMETER_RANGE_LARGE)
                    {
                        slices = (SLICES_MID_DIAMETER);
                    }
                    else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
                    {
                        slices = (SLICES_HIGH_DIAMETER);
                    }
                    else
                    {
                        slices = (SLICES_GIANT_DIAMETER);
                    }

                    Console.Clear(); // clears console to show new output lines

                    //OUTPUT

                    Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter);
                    Console.WriteLine("\n==============================================");

                    for (int slicesAddFour = 8; slicesAddFour <= slices; slicesAddFour += 4) // for each slices
                    {
                        // determine the area of the slices
                        radius = diameter / 2; // uses diameter to get radius
                        circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area  
                        areaOfSlice = Math.Round((circleArea / slicesAddFour), 2); // divides circle area by slices, takes the result of above calculation and rounds


                        Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.", slicesAddFour, areaOfSlice);


                    }


                } //end of else

                Console.Write("\nPlease enter the diameter of your pizza (0 to exit): ");
                diameter = Convert.ToDouble(Console.ReadLine());
                Console.Clear(); // clears console to show new output lines

                // set the need input value back to true for new entry
                needInput = true;

             } // end of while loop
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文