如何在Visual Studio 2017中修复C#错误cs0103? [英] How do I fix C# Error cs0103 in Visual Studio 2017?

查看:254
本文介绍了如何在Visual Studio 2017中修复C#错误cs0103?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目,该项目根据用户输入的值创建一个简单的周长和面积计算器. (用于查找窗口周长和玻璃区域).但是,我遇到了4个错误,所有错误均为CS0103.谁能帮助我修复这些错误或清理代码.我正在尝试将所有内容分离为方法,因此我想将代码保留为该常规格式.

I am working on a project that creates a simple perimeter and area calculator based on the values the user inputs. (For finding window perimeter and glass area). However, I'm stuck with 4 errors... all of which are CS0103. Can anyone help me fix these errors or clean up my code. I'm trying to separate everything into methods so I would like to keep the code in that general format.

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

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            double totalLength, totalWidth, windowPerimeter, glassArea;

            //display instructions
            DisplayInstructions();

            // ask for width
            totalWidth = AskDimension();

            //ask for lenght
            totalLength = AskDimension();

            // calculate window Perimeter
            windowPerimeter = (2 * totalWidth) * (2 * totalLength);

            //calculate the area of the window & display output
            glassArea = totalWidth * totalLength;

            //calculate and display outputs

            Console.WriteLine("the lenth of the wood is " + windowPerimeter + " feet");
            Console.WriteLine("the area of the glass is " + glassArea + " square feet");
            Console.ReadKey();

            Console.ReadKey();
        }

        //display instructions
        public static void DisplayInstructions()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("This app will help you calculate the amount of wood and glass needed for your new windows!");
            Console.WriteLine("   ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("*Note* Please enter a height/width value between 0.01 - 100, all other values will cause a system error.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("   ");
        }

        //ask for width
        public static double AskDimension()
        {
            double totalWidth;
            const double MAX_HEIGHT = 100.0;
            const double MIN_Height = 0.01;
            string widthString;
            Console.WriteLine("please enter your height of the window");
            widthString = Console.ReadLine();
            totalWidth = double.Parse(widthString);
            if (totalWidth < MIN_Height)
            {
                Console.WriteLine("you enter vaule less than min width \n" + "using minimum one");
                totalWidth = MIN_Height;
            }
            if (totalWidth > MAX_HEIGHT)
            {
                Console.WriteLine("you enter value grater than Max height\n" + "using maximum one");
                totalWidth = MAX_HEIGHT;
            }

            return AskDimension();
        }

        //ask for height
        public static double AskDimension(string dimension)
        {
            double totalLength;
            const double MAX_HEIGHT = 100.0;
            const double MIN_Height = 0.01;
            string heightString;
            Console.WriteLine("please enter your height of the window");
            heightString = Console.ReadLine();
            totalLength = double.Parse(heightString);
            if (totalLength < MIN_Height)
            {
                Console.WriteLine("you enter vaule less than min width \n" + "using minimum one");
                totalLength = MIN_Height;
            }
            if (totalLength > MAX_HEIGHT)
            {
                Console.WriteLine("you enter value grater than Max height\n" + "using maximum one");
                totalLength = MAX_HEIGHT;
            }

            return AskDimension();
        }
        //calculate and display outputs
        public static double AskDimesnion(string windowPerimeter,
                                          string glassArea,
                                          string widthString,
                                          string heightString)

        {

            windowPerimeter = 2 * (totalWidth + totalLength);
            glassArea = (totalWidth * totalLength);
            Console.WriteLine("the lenth of the wood is " + windowPerimeter + " feet");
            Console.WriteLine("the area of the glass is " + glassArea + " square feet");
            Console.ReadKey();
            return AskDimension();
        }
}
}

方法中的错误的屏幕截图:

Screenshot of errors in the method:

推荐答案

最初的问题是您的变量未在类范围内,而且计算对我来说似乎有点疯狂.但是从将您的代码粘贴到我的编辑器后,我很快便进行了整理,并得出以下结果:

The initial problem was that your variables weren't class scoped and also the calculations seem a bit crazy to me. But from pasting your code in to my editor I've had a quick tidy up and come out with the following:

using System;

namespace TestConsoleApplication
{
    class Program
    {
        static double _totalLength, _totalWidth, _windowPerimeter, _glassArea;

        static void Main(string[] args)
        {
            DisplayInstructions();
            _totalWidth = AskDimension("Height");
            _totalLength = AskDimension("Width");
            _windowPerimeter = (2 * _totalWidth) + (2 * _totalLength);
            _glassArea = _totalWidth * _totalLength;
            Console.WriteLine("The length of the wood is " + _windowPerimeter + " feet");
            Console.WriteLine("The area of the glass is " + _glassArea + " square feet");
            Console.ReadKey();
        }

        private static void DisplayInstructions()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("This app will help you calculate the amount of wood and glass needed for your new windows!");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("*Note* Please enter a height/width value between 0.01 - 100, all other values will cause a system error.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
        }

        private static double AskDimension(string dimensionType)
        {
            const double maxDimension = 100.0;
            const double minDimension = 0.01;

            Console.WriteLine($"Please enter your {dimensionType} of the window");

            var dimensionString = Console.ReadLine();
            var dimension = double.Parse(dimensionString);

            if (dimension < minDimension)
            {
                DisplayDimensionErrorAndExit("Min");
            }
            else if (dimension > maxDimension)
            {
                DisplayDimensionErrorAndExit("Max");
            }

            return dimension;
        }

        private static void DisplayDimensionErrorAndExit(string dimensionToShow)
        {
            Console.WriteLine($"You entered a value less than {dimensionToShow} dimension");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
            Environment.Exit(0);
        }
    }
}

如果用户输入了无效的输入,则程序将终止,除了可以进行计算并显示正确的输出. 如果您对此有任何疑问,请问我:-)

If a user enters invalid input the program will terminate, apart from that the calculations work and the correct output displayed. If you've got any questions about it at all just ask me :-)

这篇关于如何在Visual Studio 2017中修复C#错误cs0103?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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