2011-01-27 34 views
2

自分でC#を学ぶのに役立つ温度コンバータを構築しようとしています。私は基本のほとんどを知っているだけで、これは私が今までに思いついたものです。私が悩んでいるのは、ユーザーが入れた番号を取って、ユーザーが以前に入力した選択肢に変換することです。これは、farenheitまたはcelsiusです。繰り返しますが、私は基礎だけは知っていますが、助けてくれれば幸いです。これはしかし、あなたの質問に答えた場合、あなたは華氏に20℃を変換したい意味する「摂氏、20」のようなものを入力してくださいと仮定セルフティーチング:初心者が温度コンバータを作成しようとしています

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("What sort of temperature would you like to convert?"); 
      string tempType = Console.ReadLine(); 
      ConvertChoice(tempType.ToLower()); 
      Console.WriteLine("Please enter a temperature to convert: "); 
      string temperatureString = Console.ReadLine(); 
      int temperature = int.Parse(temperatureString); 
      Console.ReadLine(); 
     } 

     static void ConvertChoice(string tempType) 
     { 
      switch (tempType) 
      { 
       case "farenheit": 
        Console.WriteLine("Farenheit it is!"); 
        return; 
       case "celsius": 
        Console.WriteLine("Celsius it is!"); 
        return; 
       default: 
        Console.WriteLine("Invalid type, please type farenheit or celsius."); 
        return; 
      } 
     } 
    } 
} 

答えて

0

は、あなたがわからないこの

if type == fahrenheit 
    result = [formula from fahrenheit to celsius, using 'temperature'] 
    restype = "celsius" 
else 
    result = [formula from celsius to fahrenheit, using 'temperature'] 
    restype = "fahrenheit" 

print "The result is", result, "degrees", restype 

のようないくつかのロジックが必要です。

ケンビンをサポートするのにも理想的です。ユーザーが入力したものから入力温度をケルビンに変換し、ケルビンをユーザーが望むものに変換します。あなたが利点が表示されない場合は、

any unit -> kelvin -> any other unit 

どのようにあなたが代わりに5つのまたは10の異なる単位のためにこれをコーディング想像:その後、個別ケースを処理することなく、ユニットのいずれかの種類から/への変換をサポートすることができますちょうど2.

0

あなたはtempTypeに格納された選択肢を持っています。それを使用してください。

static double GetTemp(string tempChoice, int temperature) 
{ 
    double convertedTemp = 0.0; 

    if(tempChoice.Equals("farenheit", StringComparison.CurrentCultureIgnoreCase)) 
    { 
     convertedTemp = ((double)temperature * 9.0/5.0) + 32.0; 
    } 
    else 
    { 
     convertedTemp = ((double)temperature -32.0) * 5.0/9.0; 
    } 

    return convertedTemp; 
} 

main()からこの関数を呼び出してください。

(注:これはその機能に制限があり、2つの温度スケールしかないことを前提としていますが、OPはプログラミングを学んでいたため、可能な限り簡単な例を示しました)。

EDIT は私のアルゴリズムを修正しました。論理は実際に意図したとおりに動作します。

0

どのようにこれを試合しますか?

オブジェクトのアプローチを使用して
namespace ConsoleApplication1 
{ 
    // Using an enum to store the result of 
    // parsing user input is good practice. 
    public enum Scale 
    { 
     Unknown, 
     Celsius, 
     Farenheit 
    } 


    class Program 
    { 

     static void Main(string[] args) 
     { 
      Console.WriteLine("What sort of temperature would you like to convert?"); 
      string tempType = Console.ReadLine(); 

      switch(ConvertChoice(tempType)) 
      { 
       case Scale.Celsius: 
       // do celsius work here 
       break; 
       case Scale Farenheit: 
       // do farenheit work here 
       break; 
       default: 
       // invalid input work here 
      } 
      Console.ReadLine(); 
     } 

     static Scale ConvertChoice(string tempType) 
     { 
      // use the framework. also, when dealing with string equality, its best 
      // to use an overload that uses the StringComparison enum. 
      if(tempType.StartsWith("f", StringComparison.CurrentCultureIgnoreCase)) 
      return Scale.Farenheit; 
      if(tempType.StartsWith("c", StringComparison.CurrentCultureIgnoreCase))) 
      return Scale.Celsius; 
      return Scale.Unknown; 
     } 
    } 

}

0

....いくつかの可能性が高い構文/スタイルの誤差はご容赦、通常は#自分cを使用していない...

class TempConverter 
{ 
    public string degreeType {get; set;} 
    public double userTemp {get; set;} 

    public TempConverter(){} 

    public double convert() 
    { 
    switch(this.degreeType) 
    { 
     case "F": 
      return this.convertToF(); 
     case "C": 
      return this.convertToC(); 
     default: 
      return null; 
    } 

    } 
    public double convertToF() 
    { 
     return //user temp converted to F 
    } 

    public double convertToC() 
    { 
     return //user temp converted to C 
    } 
} 

次に、あなたのメインクラスです

class Program 
    { 
     static void Main(string[] args) 
     { 
     TempConverter converter = new TempConverter(); 
      Console.WriteLine("What sort of temperature would you like to convert?"); 
      converter.degreeType = Console.ReadLine(); 
      ConvertChoice(converter.degreeType); 
      Console.WriteLine("Please enter a temperature to convert: "); 
      converter.userTemp = Convert.ToDouble(Console.ReadLine()); 
      Console.WriteLine(Double.ToString(converter.convert()); 
     } 

     static void ConvertChoice(string tempType) 
     { 
      switch (tempType) 
      { 
       case "farenheit": 
        Console.WriteLine("Farenheit it is!"); 
        return; 
       case "celsius": 
        Console.WriteLine("Celsius it is!"); 
        return; 
       default: 
        Console.WriteLine("Invalid type, please type farenheit or celsius."); 
        return; 
      } 
     } 
    } 
0

プログラムにはいくつかの欠点があります。まず、ユーザーが実行したいコンバージョンのタイプを保存する必要があるため、変換が必要な温度になったときに実際に実行することができます。華氏と摂氏の2つの温度タイプ(ええと、Réaumurを使用している人は誰ですか?)のみで操作しているため)、華氏が選択されたかどうかを示すブール値としてユーザーの選択肢を保存できます。小数点を受け入れることもできます。私は、正しい値がされるまでループすることによって、両方の温度の種類と温度値のために入力されていることを確認してきた

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      bool isFahrenheit; 
      bool temperatureTypeHasBeenDetermined = false; 
      while(!temperatureTypeHasBeenDetermined){ 
       Console.WriteLine("What sort of temperature would you like to convert?"); 
       string tempType = Console.ReadLine(); 
       temperatureTypeHasBeenDetermined = ConvertChoice(tempType.ToLower(), out isFahrenheit); 
      } 
      decimal temperature; 
      bool temperatureEnteredCorrectly = false; 
      while(!temperatureEnteredCorrectly){ 
       Console.WriteLine("Please enter a temperature to convert: "); 
       string temperatureString = Console.ReadLine(); 
       temperatureEnteredCorrectly = decimal.TryParse(temperatureString, out temperature); 
      } 
      //Now we are ready to do the conversion 
      decimal convertedTemperature = isFahrenheit ? 
              ConvertFromFahrenheitToCelsius(temperature) : 
              ConvertFromCelsiusToFahrenheit(temperature); 
      string from = isFahrenheit ? "F" : "C"; 
      string to = isFahrenheit ? "C" : "F"; 

      Console.WriteLine("{0}{1} = {2}{3}", temperature, from, convertedTemperature, to);     

      Console.ReadLine(); 
     } 

     static decimal ConvertFromFahrenheitToCelsius(decimal temperature) 
     { 
      //Implement properly 
      return 60m; 
     } 

     static decimal ConvertFromCelsiusToFahrenheit(decimal temperature) 
     { 
      //Implement properly 
      return 42m; 
     } 

     static bool ConvertChoice(string tempType, out bool isFahrenheit) 
     { 
      isFahrenheit = false; 
      switch (tempType) 
      { 
       case "fahrenheit": 
        Console.WriteLine("Fahrenheit it is!"); 
        isFahrenheit = true; 
        return true; 
       case "celsius": 
        Console.WriteLine("Celsius it is!"); 
        return false; 
       default: 
        Console.WriteLine("Invalid type, please type fahrenheit or celsius."); 
        return false; 
      } 
     } 
    } 
} 

注:

ので、ここであなたは私の提案を反映するようにプログラムを変更することができる方法だ、と述べました有効な値が得られる。

私はこれが正しい自己学習のための正しい方向にあなたを導いてくれることを願っています。上記についてご不明な点がございましたら、お気軽にお問い合わせください。免責事項として、私は上記のコードをコンパイルしていないと言わなければなりませんが、私のメンタルシンタックスチェッカーは通常かなり信頼できます;-)

関連する問題