C# 为什么可以';我不能在我的主程序中访问我的类变量吗?
正如我在标题中所说,我无法从类中访问变量。最初,错误是“CS0122:“Game.win”由于其保护级别而无法访问。“在查看其他有类似错误的人后,我对代码进行了一些更改,修复了CS0122错误,但这引入了一个新错误,“CS0120:非静态字段、方法或属性“Game.win”需要对象引用。” 我也查过这个错误,但我似乎无法理解人们发布的解决方案。下面是我的主要程序代码C# 为什么可以';我不能在我的主程序中访问我的类变量吗?,c#,C#,正如我在标题中所说,我无法从类中访问变量。最初,错误是“CS0122:“Game.win”由于其保护级别而无法访问。“在查看其他有类似错误的人后,我对代码进行了一些更改,修复了CS0122错误,但这引入了一个新错误,“CS0120:非静态字段、方法或属性“Game.win”需要对象引用。” 我也查过这个错误,但我似乎无法理解人们发布的解决方案。下面是我的主要程序代码 using System; using static System.Console; namespace ConsoleApp1
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);
}
}
}
我读到的解决方案提到了从方法或对象中删除“静态”,但我的变量不在方法中,因此我不确定我应该做什么,我非常希望从这里得到一些关于如何继续的建议。实际上,这里有几个问题。第一个是
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);
}
这里不需要任何static
(当然除了Main
和GetInput
)
只是一个辅助:从代码中的任意位置退出通常是一个坏主意,环境也是如此。退出
。您可以从游戏
-方法返回一个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();
}
试试这个
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;
}
}
公共类程序
{
公共静态游戏game1{get;set;}
静态void Main(字符串[]参数)
{
game1=新游戏();
game1.SetQuestion1(GetInput());
game1.game();
game1.DisplayAll();
}
公共静态字符串GetInput()
{
字符串输入=”;
做
{
WriteLine(“请输入您的选择:石头-1,布-2,剪刀-3”);
输入=读线();
}
而(game1.win<4 | | game1.lose<4 | | game1.usermoney==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");
}
}
}
}
名称空间控制台App12
{
班级计划
{
静态void Main(字符串[]参数)
{
Game game1=新游戏();
游戏1.设置问题1(获取输入(游戏1));
game1.game();
game1.DisplayAll();
控制台。WriteLine(“游戏结束”);
Console.ReadKey();
}
公共静态字符串GetInput(游戏)
{
做
{
字符串输入;
WriteLine(“请输入您的选择:石头-1,布-2,剪刀-3”);
input=Console.ReadLine();
返回输入;
}
而(game.win<4 | | game.lose<4 | | game.usermoney==0);
}
}
}
名称空间控制台AP12
{
班级游戏
{
公共双用户货币=100;
公共双赢=0;
公共双输=0;
公共双用户选择,计算机选择;
公开无效游戏()
{
if(userchoice==1&&computerchoice==3)
{
win++;
用户货币=用户货币+20;
}
else if(userchoice==2&&computerchoice==1)
{
win++;
用户货币=用户货币+20;
}
else if(userchoice==3&&computerchoice==2)
{
win++;
用户货币=用户货币+20;
}
else if(userchoice==0)
{
环境。退出(0);
}
其他的
{
丢失++;
usermoney=usermoney-10;
}
}
公共双设置问题1(字符串参数1)
{
userchoice=double.Parse(param1);
返回用户选择;
}
公共双计算机()
{
随机rnd=新随机();
computerchoice=rnd.Next(1,3);
返回计算机选择;
}
public void DisplayAll()
{
WriteLine(“用户选择:“+userchoice+”,计算机选择:“+computerchoice+”。记住,1=石头,2=布,3=剪刀”);
Console.WriteLine(“用户余额为”+usermoney);
if(userchoice==1&&computerchoice==3)
{
Console.WriteLine(“用户赢-+20美元”);
}
else if(userchoice==2&&computerchoice==1)
{
Console.WriteLine(“用户赢-+20美元”);
}
else if(userchoice==3&&computerchoice==2)
{
Console.WriteLine(“用户赢-+20美元”);
}
其他的
{
Console.WriteLine(“用户损失--10美元”);
}
}
}
}
您试图访问作为游戏类成员的'win'变量,您需要创建游戏类的对象,然后才能通过该对象访问它,否则您需要将'win'变量设置为静态,因为只有静态变量才能像这样访问。i、 e(ClassName.variableName)您的变量不在方法中,而是在类中,您需要将它们设置为静态,以便通过它们的类名直接访问:)游戏1的game1
应该是一个静态字段,您可以从中读取,而loopC#是一种面向对象的语言,您试图访问的Game.win
成员是Game
对象的字段。由于它们不是静态的,您必须首先初始化游戏
类的实例。如果这是一个愚蠢的问题,我很抱歉
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;
}
}
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");
}
}
}
}