如何在一种方法中设置一个值,然后 return 该值到另一种方法?

How do I set a value in one method and then return that value to another method?

我需要一些帮助。如果我没理解错的话,“int F”的值会被发送到“FtoC”并进行转换,然后 returned 到 MenuSelect1 方法。 现在我想 return 或保存转换为 MenuSelect2 方法后的值“int C”?我已经尝试了我能想到的一切,但我只是遇到错误。 (我现在已将代码重置为原始状态,其中 MenuSelect1 和 2 är 无效)。解决这个问题的简单方法是什么?谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


  public static class SecondClass {


  public static int FtoC(int fahrenheit)
  {
    int C = ((fahrenheit - 32) * 5) / 9;
    return C;
  }

  
  public static void MenuSelect1()
  {
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      int C = SecondClass.FtoC(F);

      if (C < 80 || C > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(C + "°C is perfect. Start the sauna and enjoy!");
        break;
      }
    }
    Console.ReadKey();
  }

   
  public static void MenuSelect2()
  {
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write("Temperature: {0}");
    Console.ReadKey();
  }
    

}

您可以直接从 MenuSelect1()

调用 MenuSelect2()
public static void MenuSelect1()
{
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      int C = SecondClass.FtoC(F);

      if (C < 80 || C > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(C + "°C is perfect. Start the sauna and enjoy!");
        
        // here
        MenuSelect2(C);
        
        break;
      }
    }
    Console.ReadKey();

}

public static void MenuSelect2(int C)
{
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write($"Temperature: {C}");   // <- notice the $ for interpolated strings.
}

或 return 从 MainSelect1() 到调用 MainSelect2() 方法的调用者的值。

public static void Main()
{
    // get the temperature
    int C = MenuSelect1();

    // pass it to the other method.
    MenuSelect2(C);

    Console.ReadKey();
}


public static int MenuSelect1()  // <- change the signature (return int)
{
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      int C = SecondClass.FtoC(F);

      if (C < 80 || C > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(C + "°C is perfect. Start the sauna and enjoy!");
        
        return C; // return the value to the caller.
      }
    }
}

public static void MenuSelect2(int C)
{
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write($"Temperature: {C}");   // <- notice the $ for interpolated strings.
}

我宁愿使用第二个建议,因为这样 MenuSelect1() 就不会与 MenuSelect2() 紧密耦合,可以重复用于其他目的。

此时的问题是变量 C 被实例化并存储在方法 MenuSelect1() 中,您应该做的是创建一个 class 变量,如 int fahrenheitValue 然后在 menuSelect1 中方法使用 this.fahrenheitValue = C 所以值存储在 class 变量中然后你可以从任何地方访问它

方法结束后,MenuSelect1() 中定义的所有变量将不可见。

您可以在“SecondClass”中定义静态 属性

private static int degreesCelsius;

然后你可以在

中设置这个属性
if (C < 80 || C > 87)
{
    Console.WriteLine("Try a different temperature.");
}
else
{
    Console.WriteLine(C + "°C is perfect. Start the sauna and enjoy!");
    degreesCelsius = C;
    break;
}

在您的 MenuSelect2() 方法中您可以使用它。

Console.Write("Temperature: {0}", degreesCelsius);

您还可以将 C 的值保存为 class 中的 属性 / 字段。这是将其保存为名为 _celcius:

的字段的示例
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


  public static class SecondClass {

  private static int _celcius = 0;  // <--- Your field

  public static int FtoC(int fahrenheit)
  {
    int C = ((fahrenheit - 32) * 5) / 9;
    return C;
  }

  
  public static void MenuSelect1()
  {
    while (true)
    {
      int F = 0;
      Console.Write("\nType how many degrees Fahrenheit you want: ");

      try
      {
        F = Convert.ToInt32(Console.ReadLine());
      }
      
      catch (Exception e)
      {
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("ERROR!");
        Console.ForegroundColor = ConsoleColor.White;
      }

      _celcius = SecondClass.FtoC(F);   // <--- Assign field here

      if (_celcius  < 80 || _celcius  > 87)
      {
        Console.WriteLine("Try a different temperature.");
      }
      else
      {
        Console.WriteLine(_celcius + "°C is perfect. Start the sauna and enjoy!");
        break;
      }
    }
    Console.ReadKey();
  }

   
  public static void MenuSelect2()
  {
    Console.WriteLine("Starting Sauna. Wait for it to reach desired temperature...");
    Console.Write("Temperature: {0}", _celcius); // <--- Use your field here
    Console.ReadKey();
  }
    

}```