我试图在C#中的方法之间“共享”变量。我是C#的新手,我知道没有“全局变量”这样的东西,我不太确定如何正确使用静态变量。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectOne
{
static bool tooHigh;
static internal waistMeasurment;
class Main
{
static void Main(string[] args)
{
GetVariables();
}
public static void GetVariables() //This method gets the users height & waist measurments
//and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
{
Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
waistMeasurment = Console.ReadLine(); //This writes the users input under the string 'waistMeasurment'
ValidateWaist();
if tooHigh ==true
{
waistMeasurment = Console.ReadLine();
ValidateWaist();
}
}
public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
{
if (waistMeasurment < 60) //Checks the lower bound of the waist limit
{
Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
tooHigh = true;
}
else
{
tooHigh = false;
}
}
}
}
我有太高和腰部的问题
答案 0 :(得分:2)
C#是一种面向对象的编程语言,这意味着它由namespaces
组成,其中包含classes
和structs
,其中包含fields
和properties
(所以,变量)和methods
。
你是绝对正确的,C#中没有全局变量。虽然,你可以将一个带有静态变量的类破解并将其用作全局变量,但最好将所有变量保持在本地并受到控制。
您希望实现的目标是将两个变量(tooHigh
和waistMeasurment
)作为静态变量置于静态Main
类中。
你也不会以Python的风格使用if
语句,internal
是为方法而不是变量而发明的。根据您的代码,您正在寻找integer
的类型,因为稍后,您正在检查变量waistMeasurment
是否小于60.为了做到这一点,您首先必须投射变量为integer
。正确的方法是使用int.TryParse
方法。
namespace ProjectOne
{
class Main
{
static bool tooHigh;
static int waistMeasurment;
static void Main(string[] args)
{
GetVariables();
}
public static void GetVariables() //This method gets the users height & waist measurments
//and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
{
Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
int.TryParse(Console.ReadLine(), out waistMeasurment); //This writes the users input under the string 'waistMeasurment'
ValidateWaist();
if (tooHigh)
{
int.TryParse(Console.ReadLine(), out waistMeasurment);
ValidateWaist();
}
}
public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
{
if ((int)waistMeasurment < 60) //Checks the lower bound of the waist limit
{
Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
tooHigh = true;
}
else
{
tooHigh = false;
}
}
}
}
答案 1 :(得分:0)
如评论中所述,您必须更改代码才能将变量置于Type
内:
你得到了什么:
namespace ProjectOne
{
static bool tooHigh;
static internal waistMeasurment;
class Main
{
//your code
}
}
你要写的是什么:
namespace ProjectOne
{
class Main
{
static bool tooHigh;
static internal waistMeasurment;
//your code again
}
}
答案 2 :(得分:0)
静态可用于共享变量,但需要在Class中声明,程序中有更多错误,我修改了程序,你可以使用它并调试它用于学习
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static bool tooHigh;
static int waistMeasurment;
static void Main(string[] args)
{
GetVariables();
}
public static void GetVariables() //This method gets the users height & waist measurments
//and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
{
Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
int.TryParse(Console.ReadLine(), out waistMeasurment); //This writes the users input under the string 'waistMeasurment'
ValidateWaist();
if (tooHigh == true)
{
int.TryParse(Console.ReadLine(), out waistMeasurment); ;
ValidateWaist();
}
}
public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
{
if (waistMeasurment < 60) //Checks the lower bound of the waist limit
{
Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
tooHigh = true;
}
else
{
tooHigh = false;
}
}
}
}