错误解码帮助 [英] error decoding help

查看:109
本文介绍了错误解码帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在performMenueOption()输入上不断遇到两个错误
对于lengthInMeters和widthInMeters我都收到此错误,
错误1使用未分配的局部变量"lengthInMeters"
错误2使用未分配的局部变量''widthInMeters''
有人可以帮帮我吗.
谢谢(对不起代码转储)

I keep getting two errors on the performMenueOption() inputs
for both lengthInMeters and widthInMeters i get this error,
Error 1 Use of unassigned local variable ''lengthInMeters''
Error 2 Use of unassigned local variable ''widthInMeters''
can someone please help me out.
thanks (sorry for code dumping)

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {

            int menuOption;
            double lengthInMeters;
            double widthInMeters;
            do
            {
                MainMenu();
                menuOption = ReadOption();
                PerformMenuOption(menuOption, lengthInMeters, widthInMeters);


            } while (menuOption != 4);

        }
        /// Displays Main menu
        /// precondition: true
        /// postcondition: displays the main menu to the screen
        public static void MainMenu()
        {
            string mainMenu = "\n\n=======================================================================\n"
                            + "\n1) Calculate cost of carpeting a room with particular carpet\n"
                            + "2) Compare cost of carpeting a room with two different types of carpet\n"
                            + "3) Enter/Change room dimensions\n"
                            + "4) Exit\n"
                            + "Please enter your selection 1-4\n\n"
                            + "========================================================================\n";

            Console.Write(mainMenu);
        }// end MainMenu

        /// Reads users input
        /// Preconditions: true
        /// Postcondition:returns options which is either 1,2,3 or 4.
        static int ReadOption()
        {

            int option;
            bool ValidMenuOption = false;

            do
            {
                while (!Int32.TryParse(Console.ReadLine(), out option))
                {
                    Console.WriteLine("Please try again with a valid menu option.");
                }

                if ((1 <= option)
                    && (option <= 4))
                {
                    ValidMenuOption = true;
                }
                else
                {
                    ValidMenuOption = false;
                } //end if

                if (!ValidMenuOption)
                {
                    Console.WriteLine("\n\t You have entered an invalid option please enter 1, 2, 3 or 4");
                    MainMenu();
                } //end if
            } while (!ValidMenuOption);

            return option;
        }//end ReadOption


        /// reads dimensions of the room
        /// Precondition: true
        /// Pastcondition: returns the cost of carpeting the room.????

        static void PerformMenuOption(int option, double lengthInMeters, double widthInMeters)
        {

            double value;
            if (option == 1)
            {

                int carpetMenuOption;
                bool dimensions = DimensionsEntered(lengthInMeters, widthInMeters);
                do
                {
                    bool previousDimensions;
                    previousDimensions = DimensionsEntered(lengthInMeters, widthInMeters);
                    if (previousDimensions == false)
                    {
                       ReadLength();
                       ReadWidth();

                    }

                    CarpetMenu();
                    carpetMenuOption = ReadCarpetOption();

                    value = CalculateCarpetCost(lengthInMeters, widthInMeters, carpetMenuOption);


                } while (carpetMenuOption != 5);

                MainMenu();


            }
            else if (option == 2)
            {
             // to be compleated
            }
            else if (option == 3)
            {
            // to be compleated

            }
            else
            {
                // need to complete
                DateTime date = DateTime.Now;
                Console.WriteLine("\n Today's Date and Time: {0}\n", date);


            }
        }

        /// Reads length of room
        /// Preconditions:true
        /// Postconditions:returns length in meters
        static double ReadLength()
        {
            double lengthInMeters;

            do
            {
                Console.Write("Please eneter the length of the room you wish to carpet in meters:");

                while (!double.TryParse(Console.ReadLine(), out lengthInMeters))
                {
                    Console.WriteLine("Please try again with a valid length option.");
                }

                if (lengthInMeters < 2.4 || lengthInMeters > 4.5)
                {
                    Console.Write("\nYou have entered an illegal length, the dimensions of the room must not be larger than 4.5m or smaller than 2.4m."
                            + "\n Please enter the length of your room in meters again");
                }//end if

            } while (lengthInMeters < 2.4 || lengthInMeters > 4.5);
            return lengthInMeters;
        }// end ReadLength


        /// Reads Width of room
        /// Preconditions: true
        /// Postconditions: returns the width of the room
        static double ReadWidth()
        {
            double widthInMeters;

            do
            {
                Console.Write("Please eneter the width of the room you wish to carpet in meters:");
                //  widthInMeters = double.Parse(Console.ReadLine());

                while (!double.TryParse(Console.ReadLine(), out widthInMeters))
                {
                    Console.WriteLine("Please try again with a valid width option.");
                }


                if (widthInMeters < 2.4 || widthInMeters > 4.5)
                {
                    Console.Write("\nYou have entered an illegal width, the dimensions of the room must not be bigger then 4.5m or smaller than 2.4m."
                            + "\n please enter the width of your room in meters again");
                }//end if

            } while (widthInMeters < 2.4 || widthInMeters > 4.5);
            return widthInMeters;

        }//end ReadWidth

        /// checks to see if dimensions have been entered
        /// Preconditions: false
        /// postconditions:returns haveDimensionsBeenEntered
        public static bool DimensionsEntered(double lengthInMeters, double widthInMeters)
                    {

            bool haveDimensionsBeenEntered = false;

            if (lengthInMeters > 0 && widthInMeters > 0)
            {
                haveDimensionsBeenEntered = true;
            }
            return haveDimensionsBeenEntered;
        }//end dimensions entered



        /// Displays
        /// precondition: true
        /// postcondition: displays carpet menu
        public static void CarpetMenu()
        {
            string carpetMenu = "\n\n"
                            + "\t1) Berber $39.95 per meter\n"
                            + "\t2) Frieze $34.95 per meter\n"
                            + "\t3) Pile $23.59 per metern\n"
                            + "\t4) Plush $19.99 per meter\n"
                            + "\t5) Exit\n"
                            + "Please enter your selection 1-5\n";

            Console.Write(carpetMenu);
        }// end CarpetMenu


        /// Reads users input
        /// Preconditions: true
        /// Postcondition:returns options which is either 1,2,3 or 4.
        static int ReadCarpetOption()
        {
            int carpetOption = 0;
            bool ValidCarpetOption = false;

            do
            {

                while (!Int32.TryParse(Console.ReadLine(), out carpetOption))
                {
                    Console.WriteLine("Please try again with a valid menu option.");
                }

                if ((1 <= carpetOption)
                    && (carpetOption <= 5))
                {
                    ValidCarpetOption = true;
                }
                else
                {
                    ValidCarpetOption = false;
                } //end if
                if (!ValidCarpetOption)
                {
                    Console.WriteLine("\n\t Option must be 1, 2, 3, 4 or 5");
                    CarpetMenu();
                } //end if
            } while (!ValidCarpetOption);

            return carpetOption;
        }//end ReadCarpetOption


        /// calculates the cost of carpeting a room
        /// preconditions: true
        /// postconditions: displays how much it will cost to carpet a room (with cirtain dimensions) with a specific type of carpet
        static double CalculateCarpetCost(double lengthInMeters, double widthInMeters, int carpetSelection)
        {
            const double breber = 39.95;
            const double frieze = 34.95;
            const double pile = 23.59;
            const double plush = 19.95;
            double carpetCost;
            //  previousDimensions = DimensionsEntered(ref length,ref width);
            //  if (previousDimensions == false)
            //  {
            //     length = ReadLength();
            //      width = ReadWidth();
            //  }

            switch (carpetSelection)
            {
                case 1: carpetCost = breber * lengthInMeters * widthInMeters;
                    break;
                case 2: carpetCost = frieze * lengthInMeters * widthInMeters;
                    break;
                case 3: carpetCost = pile * lengthInMeters * widthInMeters;
                    break;
                case 4: carpetCost = plush * lengthInMeters * widthInMeters;
                    break;
                default: Main();
                    carpetCost = 0;
                    break;
            }

            Console.WriteLine("The cost of carpeting the room with {0} carpet is {1:c}", carpetSelection, carpetCost);

            return carpetCost;
        }//carpet calculatro
    }



}








推荐答案

每米39.95 \ n" + "
39.95 per meter\n" + "\t2) Frieze


每米34.95 \ n" + "
34.95 per meter\n" + "\t3) Pile


每米23.59 \ n" + "
23.59 per metern\n" + "\t4) Plush


这篇关于错误解码帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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