为什么我不能在主程序中访问类变量?

时间:2019-11-28 07:30:20

标签: c#

就像我在标题中说的那样,我无法从班级访问变量。最初,错误是“ CS0122:由于其保护级别,无法访问'Game.win'”。在查看了其他遇到类似错误的人之后,我对代码进行了一些更改,从而修复了CS0122错误,但这引入了一个新错误:“ CS0120:非静态字段,方法或属性“ Game.win”。”

我也已经查找了此错误,但是我似乎对人们发布的解决方案没有任何意义。这是我下面的主要程序代码。

using System;
using static System.Console;

namespace ConsoleApp12
{
    public class Program
    {
        static void Main(string[] args)
        {
            Game game1 = new Game();
            game1.SetQuestion1(GetInput());
            game1.game();
            game1.DisplayAll();
        }
        public static string GetInput()
        {
            do
            {
                string input;
                WriteLine("Please enter your choice: Rock - 1, Paper - 2, Scissors - 3");
                input = ReadLine();
                return input;
            }
            while (Game.win < 4 || Game.lose < 4 || Game.usermoney == 0);
        }

    }
}

这是我的课程代码

using System;
using System.Collections.Generic;
using System.Text;
using static System.Console;

namespace ConsoleApp12
{
    public class Game
    {

            public double usermoney = 100;
            public double win = 0;
            public double lose = 0;
            public double userchoice, computerchoice;

        public void game()
        {
            if (userchoice == 1 && computerchoice == 3)
            {
                win++;
                usermoney = usermoney + 20;
            }
            else if (userchoice == 2 && computerchoice == 1)
            {
                win++;
                usermoney = usermoney + 20;
            }
            else if (userchoice == 3 && computerchoice == 2)
            {
                win++;
                usermoney = usermoney + 20;
            }
            else if (userchoice == 0)
            {
                Environment.Exit(0);
            }
            else
            {
                lose++;
                usermoney = usermoney - 10;
            }
        }
        public double SetQuestion1(string param1)
        {
            userchoice = double.Parse(param1);
            return userchoice;
        }
        public double Computer()
        {
            Random rnd = new Random();
            computerchoice = rnd.Next(1, 3);
            return computerchoice;
        }
        public void DisplayAll()
        {
            WriteLine("User chose: " + userchoice + ", Computer chose: " + computerchoice + ". Remember, 1 = Rock, 2 = Paper, 3 = Scissors");
            WriteLine("User balance is " + usermoney);
            if (userchoice == 1 && computerchoice == 3)
            {
                WriteLine("User Wins - +$20");
            }
            else if (userchoice == 2 && computerchoice == 1)
            {
                WriteLine("User Wins - +$20");
            }
            else if (userchoice == 3 && computerchoice == 2)
            {
                WriteLine("User Wins - +$20");
            }
            else
            {
                WriteLine("User Loses - -$10");
            }
        }
    }
}

我读过的解决方案提到从方法或对象中删除“静态”,但是我的变量不在方法中,因此我不确定应该执行的操作,并且我非常感谢有关如何进行操作的建议从这里。

3 个答案:

答案 0 :(得分:0)

您实际上在这里有几个问题。首先是Game类的实例仅存在于Main中。因此,从任何其他方法甚至类中引用它都是不可能的。这也适用于您的GetInput方法。为了使用该实例,您可以将其作为参数传递给GetInput

 public static string GetInput(Game game)
 {
     do
     {
         string input;
         WriteLine("Please enter your choice: Rock - 1, Paper - 2, Scissors - 3");
         input = ReadLine();
         return input;
     }
     while (game.win < 4 || game.lose < 4 || game.usermoney == 0);
 }

现在您要做的就是将Game的{​​{1}}实例提供给该方法:

Main

这里static void Main(string[] args) { Game game1 = new Game(); game1.SetQuestion1(GetInput(game1)); game1.game(); game1.DisplayAll(); } 不需要任何东西(当然还有staticMain)。

顺便提一句:从代码中的任意位置退出通常是个坏主意-GetInput也是如此。相反,您可以从Environment.Exit方法返回一个bool,以指示程序是否应该退出:

game

答案 1 :(得分:0)

尝试

public void game()
{
    ...
    else if (userchoice == 0)
    {
        return false;
    }
    return true;
}

static void Main(string[] args)
{
    Game game1 = new Game();
    game1.SetQuestion1(GetInput());
    if(!game1.game())
        return; // here you get the return-value of game and exit program if user typed zero.
    game1.DisplayAll();
}

答案 2 :(得分:0)

您不能访问您的类变量,因为它们是在方法内部声明的,而不是作为字段或属性声明的。在这个应用程序中,我建议将game1传递给您的GetInput方法作为参考,它将使您能够访问您的对象。

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {
            Game game1 = new Game();
            game1.SetQuestion1(GetInput(game1));
            game1.game();
            game1.DisplayAll();

            Console.WriteLine("End of game");
            Console.ReadKey();
        }

        public static string GetInput(Game game)
        {
            do
            {
                string input;
                Console.WriteLine("Please enter your choice: Rock - 1, Paper - 2, Scissors - 3");
                input = Console.ReadLine();
                return input;
            }
            while (game.win < 4 || game.lose < 4 || game.usermoney == 0);
        }
    }
}

namespace ConsoleApp12
{
    class Game
    {
        public double usermoney = 100;
        public double win = 0;
        public double lose = 0;
        public double userchoice, computerchoice;

        public void game()
        {
            if (userchoice == 1 && computerchoice == 3)
            {
                win++;
                usermoney = usermoney + 20;
            }
            else if (userchoice == 2 && computerchoice == 1)
            {
                win++;
                usermoney = usermoney + 20;
            }
            else if (userchoice == 3 && computerchoice == 2)
            {
                win++;
                usermoney = usermoney + 20;
            }
            else if (userchoice == 0)
            {
                Environment.Exit(0);
            }
            else
            {
                lose++;
                usermoney = usermoney - 10;
            }
        }
        public double SetQuestion1(string param1)
        {
            userchoice = double.Parse(param1);
            return userchoice;
        }
        public double Computer()
        {
            Random rnd = new Random();
            computerchoice = rnd.Next(1, 3);
            return computerchoice;
        }
        public void DisplayAll()
        {
            Console.WriteLine("User chose: " + userchoice + ", Computer chose: " + computerchoice + ". Remember, 1 = Rock, 2 = Paper, 3 = Scissors");
            Console.WriteLine("User balance is " + usermoney);
            if (userchoice == 1 && computerchoice == 3)
            {
                Console.WriteLine("User Wins - +$20");
            }
            else if (userchoice == 2 && computerchoice == 1)
            {
                Console.WriteLine("User Wins - +$20");
            }
            else if (userchoice == 3 && computerchoice == 2)
            {
                Console.WriteLine("User Wins - +$20");
            }
            else
            {
                Console.WriteLine("User Loses - -$10");
            }
        }
    }
}