我正在尝试制作一个程序来确定用户是否是俱乐部会员,然后根据他们的年龄显示他们的折扣金额。我编写了程序,但它给了我一些我无法找到解决方案的错误。我试过搜索这个网站和其他人找到我做错了什么,但每次排除故障都失败了。我是一名学习程序员,所以我确信这是我失踪的原因。对此问题的任何意见将不胜感激。 我知道为什么我收到关于当前上下文中不存在的年龄的错误,我只是不明白我的代码中缺少什么,以便正确处理'age'。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PA05
{
class DiscountApp
{
public static void Main(string[] args)
{
DisplayTitle();
InputMembershipStatus(age);
DetermineDiscount(age);
TerminateProgram();
}
public static void DisplayTitle()
{
Console.WriteLine("Programming Assignment 5 - Determine Discount\n\tProgrammer: ");
Console.WriteLine();
DrawLine();
}
public static void InputMembershipStatus(int age)
{
Console.WriteLine("Are you a Club Member? <Y or N>: ");
string aValue = Console.ReadLine();
if (aValue == "Y" || aValue == "y" || aValue == "Yes" || aValue == "yes")
{
age = InputAge();
}
else if (aValue == "N" || aValue == "n" || aValue == "No" || aValue == "no")
{
Console.WriteLine("Sorry, discounts apply to Club Members only.");
TerminateProgram();
}
}
public static int InputAge()
{
int age;
Console.Write("Please enter the customer's age: ");
age = Convert.ToInt32(Console.ReadLine());
return age;
}
public static double DetermineDiscount(int age)
{
double discountAmount;
if (age <= 10 || age >= 60)
{
discountAmount = .15;
Console.WriteLine("The discount is a {0:P2}", discountAmount);
}
else
{
discountAmount = .1;
Console.WriteLine("The discount is b {0:P2}", discountAmount);
}
return discountAmount;
}
public static void DrawLine()
{
Console.WriteLine("_________________________________________________________");
}
public static void TerminateProgram()
{
DrawLine();
Console.WriteLine("Press any key to terminate the program...");
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
问题是c#中的变量范围有限。您无法像在尝试那样访问在另一个函数中声明的变量。
看起来你正试图在InputMembershipStatus
中设置年龄。你想要的是 return 变量:
public static void Main(string[] args)
{
DisplayTitle();
// this age variable is declared inside Main
// and receives its value from InputMembershipStatus
int age = InputMembershipStatus();
DetermineDiscount(age);
TerminateProgram();
}
和
// this function now returns an int instead of having a parameter
public static int InputMembershipStatus()
{
int age = 0;
Console.WriteLine("Are you a Club Member? <Y or N>: ");
string aValue = Console.ReadLine();
if (aValue == "Y" || aValue == "y" || aValue == "Yes" || aValue == "yes")
{
age = InputAge();
}
else if (aValue == "N" || aValue == "n" || aValue == "No" || aValue == "no")
{
Console.WriteLine("Sorry, discounts apply to Club Members only.");
TerminateProgram();
}
return age;
}
答案 1 :(得分:0)
在Main
添加:
int age = 22; //initialize it
答案 2 :(得分:0)
变量age
在调用InputMembershipStatus(age);
时第一次使用,因此编译器假定它存在于那里声明为局部变量或全局类级别,但没有声明该变量是本地的或全局的,因此您会收到错误消息。
修复程序的第一件事是更改InputMembershipStatus
删除传入的age
(不是真的需要)并将请求的值返回给用户。常规值-1用于表示用户未输入正确的年龄值的方式。
public static int InputMembershipStatus()
{
int age = -1;
Console.WriteLine("Are you a Club Member? <Y or N>: ");
string aValue = Console.ReadLine();
if (aValue == "Y" || aValue == "y" || aValue == "Yes" || aValue == "yes")
{
age = InputAge();
}
else if (aValue == "N" || aValue == "n" || aValue == "No" || aValue == "no")
{
Console.WriteLine("Sorry, discounts apply to Club Members only.");
TerminateProgram();
}
return age;
}
然后您可以修复main
函数以获取此值并将其传递给DetermineDiscount
// Ask the age to the user and store it in a local variable here
int age = InputMembershipStatus();
// If we have received a valid value for age,
// pass that local value to the DetermineDiscount function
if(age != -1)
DetermineDiscount(age);
TerminateProgram();
最后,InputAge
方法需要另一个重要的修复方法。 (与你的主要问题无关,但很重要)
public static int InputAge()
{
int age;
Console.Write("Please enter the customer's age: ");
if(int.TryParse(Console.ReadLine(), out age))
return age;
else
return -1;
}
您需要检查用户输入。在非数字输入上使用Convert.ToInt32
将使程序崩溃并发生异常(尝试按Enter键而不进行任何输入,空白字符串将具有相同的效果)。相反,Int32.TryParse将尝试将输入转换为有效的整数值,如果成功则将返回true值并在传递的参数中返回转换后的数字。再次返回常规值-1表示无效年龄输入并终止程序