我需要一些幫助。如果我理解這一點,“int F”的值會被發送到“FtoC”轉換,然后回傳到 MenuSelect1 方法。現在我想回傳或保存轉換為 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();
}
}
uj5u.com熱心網友回復:
您可以MenuSelect2()直接從MenuSelect1()
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.
}
或者將值回傳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()并且可以重用于其他目的。
uj5u.com熱心網友回復:
此時的問題是變數C被實體化并存盤在方法MenuSelect1()中,你應該做的是創建一個類變數int fahrenheitValue,然后在menuSelect1方法中使用this.fahrenheitValue = C,因此值存盤在類變數中,然后你可以從任何地方訪問它
uj5u.com熱心網友回復:
一旦方法結束,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);
uj5u.com熱心網友回復:
您還可以將 的值保存C為類中的屬性/欄位。這是將其保存為名為的欄位的示例_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();
}
}```
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425681.html
上一篇:在C#中將Nullablelong與Long進行比較
下一篇:抽象類實作設計
