私は C# を初めて使用し、Hello World や BMI Calculator などのいくつかの機能するコンソール アプリケーションを作成しました。
私は現在、帝国からメートル法へ、またはその逆の重量コンバーターを作成しようとしていますが、ユーザーがどちらをやりたいかを選択させるのに問題があります。これは私が苦労しているコードの一部です:
decimal pounds;
decimal poundsconverted;
decimal kilo;
decimal kiloconverted;
string choice;
Console.WriteLine ("Press 1 to convert from imperial to metric");
Console.WriteLine ("Press 2 to convert from metric to imperial");
choice = Console.ReadLine();
if (choice (1))
Console.WriteLine ("Please enter the weight you would like to convert in pounds (lbs) ex. 140");
pounds = Convert.ToDecimal (Console.ReadLine());
poundsconverted=pounds/2.2;
Console.WriteLine("The weight in kilograms is:{0:F3}", poundsconverted);
if (choice (2))
Console.WriteLine ("Please enter the weight you would like to conver in kilograms (kg) ex. 80");
kilo = Convert.ToDecimal (Console.ReadLine());
kiloconverted=pounds*2.2;
Console.WriteLine("The weight in pounds is:{0:F3}", kiloconverted);
私の問題は、if
ステートメントにあります。複数のフォーマットを試しましたが、うまくいきませんでした。これを行うためのより良い方法はありますか?if
ステートメントで可能ですか?