0

我是 C# 新手,在我的教科书中,它要求我向 Program.cs 类的 main 方法添加一些代码,但没有告诉我如何操作。我是初学者,所以我只是在寻找基础知识,当我继续前进时,我会学习更高级的课程,所以请保持你的解释彻底,但要降低到第 1 天的水平。以下是我提供的代码。它不断地为我提供错误<

这是下面的代码:我应该将公共静态语音 TestIfElse 方法添加到 Program.cs 类:

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

namespace ifelse_Statement
{
    class Program
    {
        static void Main(string[] args)
        {
            TestIfElse(10);
            public static void TestIfElse(int n)
            {
                if (n < 10)
                {
                    Console.WriteLine(“n is less than 10”);
                }
                else if (n < 20)
                {
                    Console.WriteLine(“n is less than 20”);
                }
                else if (n < 30)
                {
                    Console.WriteLine(“n is less than 30”);
                }
                else
                {
                    Console.WriteLine(“n is greater than or equal to 30”);
                }
            }
        }
    }
}
4

3 回答 3

3

您的错误很简单 - 您不能在 C# 中使用嵌套函数。

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

namespace ifelse_Statement
{
    class Program
    {
        static void Main(string[] args)
        {
            TestIfElse(10);
        }
        public static void TestIfElse(int n)
        {
            if (n < 10)
            {
                Console.WriteLine(“n is less than 10”);
            }
            else if (n < 20)
            {
                Console.WriteLine(“n is less than 20”);
            }
            else if (n < 30)
            {
                Console.WriteLine(“n is less than 30”);
            }
            else
            {
                Console.WriteLine(“n is greater than or equal to 30”);
            }
        }
    }
}
于 2013-10-29T01:36:27.027 回答
0

您应该将 TestIfElse 方法的主体移到 Main 方法的主体之外。这样,它们都被认为是类的方法,并且可以按需要工作。

此外,在您的逻辑中,您需要检查范围,即小于 20 的数字也小于 30,因此您可能想要检查数字是否在 10 和 20 之间,或 20 和 30 之间,依此类推。

static void Main(string[] args)
    {
        TestIfElse(10);
    }

public static void TestIfElse(int n)
{
    if (n < 10)
    {
        Console.WriteLine(“n is less than 10”);
    }
    else if (n < 20)
    {
        Console.WriteLine(“n is less than 20”);
    }
    else if (n < 30)
    {
        Console.WriteLine(“n is less than 30”);
    }
    else
    {
        Console.WriteLine(“n is greater than or equal to 30”);
    }
}
于 2013-10-29T01:39:34.570 回答
0

Class这里是一个类的基本结构

public class MyClass // this is the declaration of the class 
{
    // this is a property, it is accessible by things outside of this class.
    public static string MyProperty { get; set; } ;
    private static string _myField; // this is a ['private] field, it is intended to store the state of the object. it cannot be accessed from outside of this class

    static void Main(string[] args)
    {
        // this is the method that gets run first so it make all of your initial calls
    }

    public static void TestIfElse(int n)
    {
        // this is another method (taught as module, operation, action, or subroutine in schools)
        // it has return type of void which is more or less "nothing". This type of behavior simply does 
        // something but doesn't return a value
    }

    public static bool IsNotPrime(int input)
    {
        // this is an actual function in that will return a single value whether its a primitive value or an
        // object. Whatever it is, there's ONE. The point is that a call to this function is now synonymous with
        // the value it returns. So for example, if this method was real, it is equivalent to 'true' so you could 
        // actually say if(IsNotPrime(8)){ // do things }
        return input % 2 == 0;
    }
}

您必须将这些方法分开。类可以有许多成员(字段、属性、方法等),一个方法可以调用另一个方法,但一个方法不能包含另一个。因此,当您学习识别这些时,如果您看到诸如 public 或 static 之类的关键字,您应该认为这些必须是类内部但在其他类成员之外的它们自己的实体。

例如,除了类声明本身,privateprivate static(或任何其他访问修饰符)在类型和标识符之前是您在声明类方法时首先看到的内容,因此不要尝试在该成员中放置类似的其他内容.

您在代码中遇到的问题主要是关于TestIfElse. 您在主函数中声明了它,这是您无法做到的。把它移到 main 外面,你应该没问题。

于 2013-10-29T01:46:42.990 回答