using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConTempConversion_Michelle
{
class Program
{
static void GetChoice(ref int ichoice)
{
Console.Write("Enter choice: ");
string input = Console.ReadLine();// get string from the user
bool result = int.TryParse(input, out ichoice);//convert input from string to ichoice int value
if (!result)
{
while (!result && ichoice > 3)
{
Console.WriteLine("Invalid value.Try again:");
input = Console.ReadLine();
result = int.TryParse(input, out ichoice);
}
}
}
static void Main(string[] args)
{
double fTemp;
double cTemp;
double convertToC;
double convertToF;
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
Console.WriteLine("1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit");
Console.WriteLine("3. Exit");
int ichoice = 0;
GetChoice(ref ichoice);
do
{
if (ichoice == 1)
{
Console.WriteLine("Enter Fahrenheit temperature: ");
fTemp = int.Parse(Console.ReadLine());
convertToC = ConvertCelcius(fTemp);
Console.WriteLine(fTemp + "Fahrenheit is " + convertToC + "Celsius");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("_________________________________________________");
GetChoice(ref ichoice);
}
if (ichoice == 2)
{
Console.WriteLine("Enter Celsius temperature: ");
cTemp = int.Parse(Console.ReadLine());
convertToF = ConvertCelcius(cTemp);
Console.WriteLine(cTemp + "Celsius is " + convertToF+ "Fahrenheit");
Console.WriteLine("Welcome to the temperature conversion application");
Console.WriteLine("____________________________________________________");
GetChoice(ref ichoice);
}
if (ichoice == 3)
{
Console.WriteLine("Thank you for using the temperature conversion application. Please come again.");
}
else
{
Console.WriteLine("Invalid choice. Please choose again!");
}
}
while (ichoice < 3);
Console.ReadKey();
}
static double ConvertCelcius(double c)
{
double f;
return f = 9.0/5.0*c + 32;
}
static double ConvertFahrenheit(double f)
{
double c;
return c = (f - 32) * (5.0/9.0);
}
}
}
我只是有這個程序的輸出問題。從C到F的轉換是正確的,但從F到C不正確。數學公式是正確的,但爲什麼輸出不是?請幫助...謝謝。C#溫度轉換(1期)
米歇爾
什麼是你的輸入和輸出?你期望結果如何?你的程序給你什麼結果? –
輸出是三個3選項,1 F隱藏到C,2 C轉換爲F,3退出程序。如果輸入這四個選項中的任何一個,比如4,5,那麼它會提醒用戶這是一個無效輸入,請重試。 – Michelle
在選擇1和2中調用相同的ConvertCelcius。必須是拼寫錯誤。 – Yuan