问题
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:

回答1:
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 :-)
回答2:
Your totalWidth
variable is not defined anywhere in your scope. It should be defined somewhere. Depending on where you exactly want to define it, you can do it internally in your AskDimension method or more globally. It seem by your logic it should be an input parameter of your method. Also you have other errors in your code. Your method is called AskDimesnion but you are calling it in your code by AskDimension (hopefully the correct method name...)
For variable scope, you can check Microsoft's own documentation
来源:https://stackoverflow.com/questions/53728438/how-do-i-fix-c-sharp-error-cs0103-in-visual-studio-2017