0

私は C# が初めてで、教科書では Program.cs クラスのメイン メソッドにコードを追加するように求められますが、その方法は示されていません。私は初心者なので、基本を探しているだけです。先に進むにつれて、より高度なレッスンを取り上げますので、説明を徹底的に行ってください。ただし、初日レベルまで下げてください。以下は私が提供したコードです。継続的にエラーが表示されます<

以下のコードは次のとおりです。 public static voice 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

ClassHere はクラスの基本構造です

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 などのキーワードが表示された場合、これらはクラス内ではあるが他のクラス メンバーの外にある独自のエンティティである必要があると考える必要があります。

たとえば、クラス宣言自体以外、privateまたはprivate static(または他のアクセス修飾子) 型と識別子の前は、クラス メソッドを宣言するときに最初に目にするものであるため、そのメンバー内にそのようなものを入れようとしないでください。 .

コードで発生している問題は、主に の宣言に関するものですTestIfElse。メイン関数内で宣言していますが、これはできません。メインの外に移動すれば問題ありません。

于 2013-10-29T01:46:42.990 に答える
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 に答える