当前上下文方法

时间:2015-12-15 05:55:59

标签: c#

我正在尝试创建一个程序,将类从类中的方法拉入另一个类。当它进入主程序时,它应该通过if语句只显示某些值。但是在主程序类中,我收到的错误是当前上下文中不存在名称###。如果有人可以看看这个并告诉我我错过了什么,我将不胜感激。我在主程序类中遇到了counter,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();
     }
 }

3 个答案:

答案 0 :(得分:0)

变量声明的问题

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

答案 1 :(得分:0)

你似乎宣布

public int die1 { get; set; }
public int die2 { get; set; }
public int counter { get; set; }        
<{1>}中的

,而不是RollClass。如果要使用它们,请在变量名称

之前编写Program的实例名称
RollClass

此外,RollClass myRollClass = new RollClass(); if (myRollClass.die1 == myRollClass.die2) { Console.WriteLine("Roll "+ myRollClass.counter + ": "+ myRollClass.die1 + " "+ myRollClass.die2); } 方法和die1, die2, counter类中的RollMethodDice似乎有“重复”的名称。这是允许的。但在这种情况下,默认情况下,需要使用RollClass关键字(this)来调用类变量,以区别于局部变量this.die1

此外,您所做的功能

die1

只返回die1。如果您想要返回其中的三个,则应创建一个包含三个变量的新public int GetDiceRoll() { return die1; //only this will be returned! return die2; //will never come here return counter; //will never come here } struct

class

你可以替换的地方

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

希望这会有所帮助!

答案 2 :(得分:0)

您的计划有几个问题。

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

总而言之,你真的需要阅读一些基本的OOP书。例如Head First C#Online tutorial from Microsoft也很有帮助。