[英]C# When I try to call the values from my variables in my final method they are set to 0. they show correctly when they are called previously
This is my first Program other than a Calculator, Trying to build something to track hours worked during the week.这是我除了计算器之外的第一个程序,试图构建一些东西来跟踪一周的工作时间。
public class Calculation
{
public static int realWorkDay;
public static float realHours;
public static string jobDone, workDayInput, workHoursInput, workMonthInput;
public static bool inputCorrect, dayInputCorrect;
public static void GetHoursInput() //Function to gather information on the Hours worked
{
Console.WriteLine("What hours did you work on this day?");
string workHoursInput = Console.ReadLine();
bool inputCorrect = float.TryParse(workHoursInput, out float realHours);
if (inputCorrect)
{
Console.WriteLine(realHours + " Hours");
}
else
{
Console.WriteLine("That is an invalid selection.");
GetHoursInput();
}
}
public static void GetMonthInput()
{
Console.WriteLine("What Month is this for? Example. May, Oct ");
string workMonthInput = Console.ReadLine();
if(workMonthInput.Length == 3)
{
Console.WriteLine("The Month of " + workMonthInput);
}
else
{
Console.WriteLine("That is an invalid selection.");
GetMonthInput();
}
}
public static void GetDateInput()
{
Console.WriteLine("What day is this for");
string workDayInput = Console.ReadLine();
bool dayInputCorrect = Int32.TryParse(workDayInput, out int realWorkDay);
if(dayInputCorrect)
{
Console.WriteLine(realWorkDay);
}
else
{
Console.WriteLine("That is an invalid selection.");
GetDateInput();
}
}
public static void GetJobDone()
{ //THIS SHOWS UP AS "On 0 , You worked 0" IN CONSOLE
Console.WriteLine("On " + workMonthInput + " " + realWorkDay + ", You worked " + realHours + " Hours");
Console.WriteLine("What did you do on this day");
jobDone = Console.ReadLine();
}
In this line:在这一行:
string workMonthInput = Console.ReadLine();
You are declaring a new local variable named workMonthInput
.您正在声明一个名为
workMonthInput
的新局部变量。 It happens to have the same name as a field you have named workMonthInput
, but it's a different variable.它恰好与您命名为
workMonthInput
的字段workMonthInput
,但它是一个不同的变量。
If you meant to assign to the existing field, just remove the type declaration:如果您打算分配给现有字段,只需删除类型声明:
workMonthInput = Console.ReadLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.