2015-12-15 74 views
1

我正在试图创建一个程序,它将int的方法从类中的方法拖入另一个类中。当它进入主程序时,它应该运行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(); 
    } 
} 
+1

请正确缩进你的代码,这使得它更容易让人阅读。无论如何,你已经在'RollClass'中定义了'die1'和'die2',但是你正试图从'Program'中读取它们。 – Rob

回答

0

你似乎宣告

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

RollClass,而不是在Program。如果你想使用它们,通过变量名

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

除了之前写RollClass的实例名称使用,似乎在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 
} 

只会返回die1。如果你想返回他们三个你应该创建一个新structclass包含三个变量

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

在那里你可以

public DiceResult diceResult { get; set; } 

更换

public int die1 { get; set; } 
public int die2 { get; set; } 
public int counter { 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); 
} 

希望这将帮助!

+0

谢谢,帮助了一堆。欣赏它。 – Bails

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

我做了这些改变,但我仍然有我以前做过的同样的错误。 – Bails

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

总而言之,你确实需要阅读一些基本的面向对象的书。例如Head First C#。并且​​也是有帮助的。

+0

感谢您的指点。不要为自己找借口,但我只是在这堂课的第二个星期,它是一种新的语言,使用一本书,提供的信息不是最好的。如果不是这样的话,我现在有一天在工作中,甚至更长的时间里都被烧毁了,但我今晚可能会发现大部分在这里发现的问题,而没有公布它。此刻,我的大脑已经非常油炸了。再次感谢您指出您所做的事情。我做了几个人发布的更改,它解决了我遇到的问题。 – Bails

相关问题