我正在一个项目,该项目根据用户输入的值创建一个简单的周长和面积计算器。 (用于查找窗口周长和玻璃区域)。但是,我遇到了4个错误,所有错误均为CS0103。谁能帮助我修复这些错误或清理代码。我正在尝试将所有内容分离为方法,因此我想将代码保留为该常规格式。
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();
}
}
}
方法错误的屏幕截图:
答案 0 :(得分:1)
您的totalWidth
变量未在范围内的任何位置定义。应该在某处定义。根据您确切要定义的位置,可以在内部使用 AskDimension 方法,也可以在更全局的范围内进行定义。从您的逻辑看来,它应该是方法的输入参数。另外,您的代码中还有其他错误。您的方法名为 AskDimesnion ,但是您正在代码中通过 AskDimension 调用它(希望正确的方法名称。 ..)
对于可变范围,您可以检查Microsoft's own documentation
答案 1 :(得分:0)
最初的问题是您的变量未在类范围内,而且计算对我来说似乎有点疯狂。但是从将您的代码粘贴到我的编辑器中之后,我进行了快速整理,并得出以下结果:
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);
}
}
}
如果用户输入了无效的输入,则程序将终止,除了可以进行计算并显示正确的输出。 如果您对此有任何疑问,请问我:-)