简体   繁体   English

C# 当我尝试在最终方法中调用变量中的值时,它们被设置为 0。它们在之前被调用时正确显示

[英]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.

相关问题 当我从Form1调用方法时,C#跨线程操作异常 - C# cross thread operation exception when I call my method from Form1 为什么我的 C# API 在服务器上工作正常,但当我尝试从任何其他客户端计算机调用它时总是失败? - Why does my C# API work fine on the server but it always fails when I try to call it from any other client machine? C#WPF当我调用窗口的show()时调用哪个方法? - C# WPF which method is called when I call a window's show()? 在这种情况下,我的析构函数何时被调用? (C#) - When is my destructor called in this circumstance? (C#) 当我调用 SendKeys 方法时,我的驱动程序没有更新 URL。 SeleniumWebDriver,C# - When I call SendKeys method, my driver doesn't update the URL. SeleniumWebDriver, C# 为什么在运行计时器以回调该方法时脚本会挂起? Unity C# - Why Does My Script Hang When I Run The Timer To Call Back The Method ? Unity C# 当我尝试调用我的方法时,为什么会出现对象引用错误? - Why do I get an object reference error when i try to call my method? 尝试在Button_Click方法C#和XAML中调用System.Argument.Exception错误 - System.Argument.Exception error when I try to call it in Button_Click method C# and XAML 从另一个.NET应用程序调用时,我的WebService c#无法正常工作 - My WebService c# doesn't work when I call it from another .NET application 在方法中使用变量时,可以将变量调用为方法的参数吗? - Can I call variables to the parameters of the method when I use them inside my method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM