为什么我无法访问主程序中的类变量?

  • 本文关键字:主程序 类变量 访问 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");
}
}
}
}

我读到的解决方案提到从方法或对象中删除"静态",但我的变量不在方法中,所以我不确定我应该做什么,我真的很感激一些关于如何从这里开始的建议。

试试这个

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

你在这里实际上有几个问题。首先是您的Game-class 的实例仅存在于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);
}

现在您所要做的就是提供从Main到该方法的Game实例:

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

这里不需要任何东西static(当然期待MainGetInput(。

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

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

无法访问类变量,因为它们在方法内部声明,而不是作为字段或属性声明。在此应用程序中,我建议将 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");
}
}
}
}

最新更新