0

我正在学习 C#,但我在简单的控制台程序中遇到了变量范围的问题。

到目前为止,该程序运行完美,但我知道在尝试引用先前实例化的变量时会遇到问题。

我试图将方法从静态更改为非静态,并且还应用了公共/私人访问但无济于事。

我只需要朝着正确的方向轻推,希望有人可以提供帮助!

我收到的错误消息是:

错误 1 ​​非静态字段、方法或属性“ConsoleApplication2.Program.game()”需要对象引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class Program
    {
        int numberToGuess;
        int numberGuessed;
        int triesRemaining;

        public void game()
        {
            Console.WriteLine("           ==Welcome to Guess My Number== \n");
            Console.WriteLine("Player 1: Please enter your number to guess between 1 and 20: \n");
            numberToGuess = int.Parse(Console.ReadLine());
            Console.WriteLine("Player 2, please enter your first guess, you have 7 tries: \n");
            numberGuessed = int.Parse(Console.ReadLine());

            if (numberGuessed == numberToGuess)
            {
                correct();
            }
            else
            {
                incorrect();
            }

        }

        public void correct()
        {
            Console.WriteLine("Congratulations, the number was in fact " + numberToGuess);
        }

        public void incorrect()
        {

        }


        static void  Main(string[] args)
        {
            game();            
        }
    }
}
4

3 回答 3

4

实例成员(那些没有用static关键字标记的)每个对象实例存在一次。对象的每个实例都有自己的副本。

另一方面,静态成员在整个类中只存在一次,并由所有对象实例共享。

所以,如果你有一堂课:

class Foo
{
   public static int Alpha { get ; set ; }
   public        int Bravo { get ; set ; }
}

无论您的Foo类创建了多少个实例,都只有一个Alpha. 任何实例方法或属性都可以直接访问静态成员。

实例成员,因为它们存在于每个实例的基础上,所以需要一个对象实例来引用它们。如果给Foo类添加一些方法:

public static int DoSomething()
{
   return Alpha * 3 ;
}

是完全有效的——方法是静态的,成员是静态的。实例方法同上:

public int DoSomethingElse()
{
  return Alpha * 3 ;
}

这样的事情会失败:

public static int AndNowForSomethingCompletelyDifferent()
{
  return Alpha * 3 + Bravo ;
}

Bravo如果不引用. Foo但是,这将起作用:

public static int AndNowForSomethingCompletelyDifferent( Foo instance )
{
  return Alpha * 3 + instance.Bravo ;
}

就像这样:

public int AndNowForSomethingCompletelyDifferent()
{
  return Alpha * 3 + Bravo ;
}

由于该方法是非静态的(实例方法),因此它具有this对其实例的隐式引用 ( ) Foo。以上完全等价于

public int AndNowForSomethingCompletelyDifferent()
{
  return Alpha * 3 + this.Bravo ;
}

Program在您的情况下,您可以在您的Main()方法中实例化该类:

public static void Main( string[] args )
{
  Program p = new Program() ;
  p.game() ;
  return ;
}

或者您可以将您的方法标记为,game()就像标记方法一样。correct()incorrect()staticMain()

希望这可以帮助!

于 2013-09-18T00:58:27.547 回答
0

static方法/字段属于用户定义的类型,而不是实例。

例如,如果您查看这段代码:

public class MyClass
{
    public static void Foo()
    {
    }
}

Foo()方法不从MyClass. 由于它是static,因此您可以从用户定义的类型本身访问它。前任:

public class Program
{
    static void Main(String[] args)
    {
        MyClass.Foo();
    }
}

由于Main()is static,您只能引用其中的静态方法或变量(这不包括从本地实例变量引用的方法)。

在您的代码中,game()游戏调用/调用的方法和字段/方法不是static,因此您只能通过对象实例访问它。当然,制作game()和所有其他字段/方法static都会产生一段工作代码。

有关static类型的更多信息,请查看此处:http: //msdn.microsoft.com/en-us/library/98f28cdx.aspx

于 2013-09-18T01:12:15.250 回答
0

我已经继续完成这些方法,它似乎工作得很好!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 namespace ConsoleApplication2
{
public class Program
{
    int numberToGuess;
    int numberGuessed;
    int triesRemaining = 7;
    string playAgain;
    string player1, player2;

    public void game()                                                                                                              //Initiates the game instance
        {
            Console.WriteLine("           ==Welcome to Guess My Number== \n");
            Console.WriteLine(" Player 1, Please enter your name: \n");
            player1 = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("                      ==Guess My Number== \n");
            Console.WriteLine("Hello " + player1 + " : Please enter your number to guess between 1 and 20: \n");
            numberToGuess = int.Parse(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("           ==Guess My Number== \n");
            Console.WriteLine(" Player 2, Please enter your name: \n");
            player2 = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Hello " + player2 + " please enter your first guess, you have 7 tries: \n");
            numberGuessed = int.Parse(Console.ReadLine());

            if (numberGuessed == numberToGuess)
            {
                Console.WriteLine("Congratulations, the number was in fact " + numberToGuess);
                newGame();
            }
            else
            {
                incorrect();
            }

        }

    public void incorrect()                                                                                                         //Method for dealing with incorrect answers
    {
        for (int x = 0; x < 6; x++)
        {
            triesRemaining--;
            Console.WriteLine("Incorrect, you have " + triesRemaining + " tries remaining \n");
            Console.WriteLine(player2 + ", please enter your next guess: \n");
            numberGuessed = int.Parse(Console.ReadLine());

            if (numberGuessed == numberToGuess)
            {
                Console.WriteLine("Congratulations, the number was in fact " + numberToGuess);
                newGame();
            }
            else { 
                //Do nothing
            }

        }

        Console.WriteLine("You have used up all your tries! You have failed. The number was: " +numberToGuess + "\n");
        newGame();
    }                                                      //Method for dealing with incorrect answers

    public void newGame()                                                                                                           //Method that gives the user the option to start a new game instance
    {
        Console.WriteLine("Would you like to play again? Type yes or no: \n");
        playAgain = Console.ReadLine();
        playAgain = playAgain.ToLower();
        if (playAgain == "yes")
        {
            Console.Clear();
            game();
        }
        else if (playAgain == "y")
        {
            game();
        }
        else
        {
            //Do nothing
        }
    }                                                        

   static void  Main(string[] args)
    {
        new Program().game();

    }
}
}
于 2013-09-18T01:48:37.253 回答