C#代码中的语法错误 [英] Syntax Errors in code for c#

查看:104
本文介绍了C#代码中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一些语法错误,不确定是什么错误.有关如何清除这些错误的任何建议?

错误如下:
错误1名称``InputMemberShipStatus''在当前上下文中不存在
错误2当前上下文中不存在名称"Parse"


代码如下:


I am having some syntax errors in my code, and not sure what is exactly wrong with it. Any suggestions as to how to clear up these errors?

The errors are as follows:
Error 1 The name ''InputMemberShipStatus'' does not exist in the current context
Error 2 The name ''Parse'' does not exist in the current context


Code is as follows:


//DiscountApp.cs
//MAIN METHOD

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

namespace PA05
{
    class DiscountApp
    {
        static void Main(string[] args)
        {
            DisplayTitle();

            string status = InputMemberShipStatus();



            if (status == "Y" || status == "Yes" || status == "y" || status == "yes")
            {
                int age = InputAge();
                Discount discount = new Discount(age);
                double discountRate = discount.DetermineDiscount();
                Console.WriteLine("The discount is {0:p}", discountRate);
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("\n\tSorry, discounts apply to club members only.");
                Console.WriteLine();
            }
            Console.WriteLine();
            TerminateProgram();
        }

            //prompts for membership status
            public static string InputMembershipStatus()
            {
                Console.Write("Club Member? <y or="" n="">: ");
                string memberStatus = Console.ReadLine();
                Console.WriteLine();
                return memberStatus;
            }

                 //prompts for age of club member     
                public static int InputAge()
                {
                    Console.Write("Please enter the customer's age: ");
                    int age = Parse(Console.ReadLine());
                    Console.WriteLine();
                    return age;
                }

                static void DisplayTitle()
                {  //change colors and display title
                   Console.BackgroundColor = ConsoleColor.White;
                   Console.ForegroundColor = ConsoleColor.Black;
                   Console.Clear();
                   Console.Write("\n\t\tProgramming Assignment 5 - DetermineDiscount Discount");
                   Console.Write("\n\n\t\t\tPromgrammer: Nancy Hernandez");
                   Console.Write("\n\t\t_____________________________________________");
                   return;
                }

        public static void TerminateProgram()
        {   //terminates program by pressing Enter key
            Console.Write("\n\n\t_________________________________________________");
            Console.WriteLine("\n\tpress any key to end the program...");
            Console.ReadLine();
        }
    }
}</y>

推荐答案

好的,这只是一个小小的改动.

更改

Alright it''s just a minor change.

Change

string status = InputMemberShipStatus();







to

string status = InputMembershipStatus();



问题是您没有仔细查看代码.必须指定方法的确切名称.

下一个可能是:



The problem was you did not look carefully at the code. Exact name of method should be specified.

The next one probably is:

int age = Parse(Console.ReadLine());







to

int age = Int32.Parse(Console.ReadLine());



我将假设您正在尝试从用户那里获取整数值.仅指定解析"将不会获得任何结果.如果没有名为"Parse"的对象,则编译器将引发错误.



I will assume that you are trying to just get integer values from user. You will not get any result by specifying Parse alone. The compiler will throw an error if you don''t have a object named as "Parse".


在InputAge()方法中,替换
In the InputAge() method, replace
int age = Parse(Console.ReadLine());

as

int age = int.TryParse(Console.ReadLine());



我在"InputMemberShipStatus"方法中没有看到任何错误.一旦解析语法得到纠正,它可能会被清除.



I don''t see any errors in the "InputMemberShipStatus" method. It''ll probably get cleared once the Parse syntax gets corrected.


这篇关于C#代码中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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