该名称在当前上下文方法中不存在

本文关键字:上下文 方法 不存在 | 更新日期: 2023-09-27 18:26:52

我正试图创建一个程序,将int从类中的一个方法拉到另一个类中。当它被拉入主程序时,它应该运行if语句,只显示某些值。不过,在主程序类中,我得到了一个错误,即当前上下文中不存在名称###。如果有人能看看这个,告诉我我错过了什么,我将不胜感激。我对主程序类中的计数器、die1和die2变量有问题。

namespace DiceRoll
{
    public class RollClass
    {
        public void RollMethodDice()
        {
            int die1, die2, counter;
            // create Random number generator
            Random rndRoll = new Random();
            // Loop that counts the # of rolls from 1 to 100
            for (counter = 1; counter <= 100; counter++)
            {
                // Random generators for each die
                die1 = rndRoll.Next(1, 7);
                die2 = rndRoll.Next(1, 7);
            }
        }
        public int GetDiceRoll()
        {
            return die1;
            return die2;
            return counter;
        }
        public int die1 { get; set; }
        public int die2 { get; set; }
        public int counter { get; set; }        
    }
    class Program
    {    
        static void Main(string[] args)
        {                
            Console.WriteLine("Welcome to the dice rolling program.");
            Console.WriteLine();
            Console.WriteLine("This program will roll dice 100 times and display the roll where doubles land.");
            Console.WriteLine();
            Console.WriteLine("Rolls that were in doubles:");
            RollClass myRollClass = new RollClass();
            myRollClass.RollMethodDice();
            if (die1 == die2)
            {
                Console.WriteLine("Roll "+ counter + ": "+ die1 + " "+ die2);
            }
        }
        // Key stroke is needed to close console window so results are visible
        Console.ReadKey();
     }
 }

该名称在当前上下文方法中不存在

您似乎声明了

public int die1 { get; set; }
public int die2 { get; set; }
public int counter { get; set; }        

RollClass中而不是在Program中。若要使用它们,请在变量名之前写入RollClass的实例名

RollClass myRollClass = new RollClass();
if (myRollClass.die1 == myRollClass.die2)
{
    Console.WriteLine("Roll "+ myRollClass.counter + ": "+ myRollClass.die1 + " "+ myRollClass.die2);
}

此外,在RollMethodDice方法和RollClass类中似乎存在die1, die2, counter的"重复"名称。这是允许的。但在这种情况下,默认情况下,需要使用this关键字(this.die1)来调用类变量,以将其与局部变量die1 区分开来

此外,您制作的功能

public int GetDiceRoll()
{
    return die1; //only this will be returned!
    return die2; //will never come here
    return counter; //will never come here
}

只会在第1天返回。如果您想返回其中的三个,您应该创建一个新的structclass,其中包含三个变量

public struct DiceResult {
    public int die1;
    public int die2;
    public int counter;
}

在哪里可以更换

public int die1 { get; set; }
public int die2 { get; set; }
public int counter { get; set; }        

带有

public DiceResult diceResult { get; set; }

把这个的功能放进去

public DiceResult GetDiceRoll()
{
    return diceResult;
}

并像这样调用Program中的变量

RollClass myRollClass = new RollClass();
if (myRollClass.diceResult.die1 == myRollClass.diceResult.die2)
{
    Console.WriteLine("Roll "+ myRollClass.diceResult.counter + ": "+ myRollClass.diceResult.die1 + " "+ myRollClass.diceResult.die2);
}

希望这会有所帮助!

变量声明的问题
for (this.counter = 1; this.counter <= 100; this.counter++)
        {
            // Random generators for each die
            this.die1 = rndRoll.Next(1, 7);
            this.die2 = rndRoll.Next(1, 7);
        }

您的程序中有几个问题。

namespace DiceRoll
{
    public class RollClass
    {
        //int die1, die2, counter;  // <-- Field of class should be defined outside method.
                                    // <-- And since you used auto generated property below, these are not needed here.
        public void RollMethodDice()
        {
            // create Random number generator
            Random rndRoll = new Random();
            // Loop that counts the # of rolls from 1 to 100
            for (counter = 1; counter <= 100; counter++) {
                // Random generators for each die
                die1 = rndRoll.Next(1, 7);
                die2 = rndRoll.Next(1, 7);
            }
        }
        public int GetDiceRoll()
        {
            return die1;
            //return die2;  // <-------- You cannot return multiple values in a method.
            //return counter;  // <----- Instead, an array point/reference is possible.
        }
        public int die1 { get; set; }
        public int die2 { get; set; }
        public int counter { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the dice rolling program.");
            Console.WriteLine();
            Console.WriteLine("This program will roll dice 100 times and display the roll where doubles land.");
            Console.WriteLine();
            Console.WriteLine("Rolls that were in doubles:");
            RollClass myRollClass = new RollClass();
            myRollClass.RollMethodDice();
            if (myRollClass.die1 == myRollClass.die2)  // <--- You need use your class instance to access the property.
            {
                Console.WriteLine("Roll " + myRollClass.counter + ": " + myRollClass.die1 + " " + myRollClass.die2);
            }
            // Key stroke is needed to close console window so results are visible
            Console.ReadKey();  // <--------- Method call should be stay inside a method, not a class.
        }
    }
}

总之,你真的需要读一些基本的面向对象的书。例如Head First C#。微软的在线教程也很有帮助。