我正在试图创建一个程序,它将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();
}
}
请正确缩进你的代码,这使得它更容易让人阅读。无论如何,你已经在'RollClass'中定义了'die1'和'die2',但是你正试图从'Program'中读取它们。 – Rob