0

我正在尝试制作一个程序来确定用户是否是俱乐部会员,然后根据他们的年龄显示他们的折扣金额。我已经编写了程序,但它给了我一些我无法找到解决方案的错误。我曾尝试搜索此站点和其他站点以查找我做错了什么,但我所做的每一个故障排除都失败了。我是一名学习型程序员,所以我确信这是我所缺少的东西。任何有关该问题的意见将不胜感激。我知道为什么我会收到有关当前上下文中不存在的年龄的错误,我只是不明白我的代码中缺少什么,以便正确处理“年龄”。

这是我的代码:

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();
        }
    }
}
4

3 回答 3

1

问题是 c# 中的变量范围有限。您无法像尝试那样访问在另一个函数中声明的变量。

看起来您正在尝试年龄设置为InputMembershipStatus. 你想要的是一个返回变量:

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;
}

我会阅读变量范围方法的返回值

于 2013-10-12T23:30:21.500 回答
0

补充Main

int age = 22; //initialize it
于 2013-10-12T23:27:59.817 回答
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会使程序崩溃并出现异常(尝试在没有任何输入的情况下按回车,空白字符串将具有相同的效果)。相反,Int32.TryParse将尝试将输入转换为有效的整数值,如果成功,将返回一个真值,并在传递的参数中返回转换后的数字。再次返回常规值 -1 以指示无效的年龄输入并终止程序

于 2013-10-12T23:28:44.027 回答