2015-07-06 183 views
2

我想通過製作一個可以爲我做化學計算的C#程序來成爲受過教育的懶惰化學學生。爲了編寫代碼,我必須很好地理解化學課上的程序。我在Visual Studio 2013上的C#代碼無法正常工作

我是新來的任何一種編程,C#是我的第一語言。 該代碼適用於1元素計算,但不適用2元素計算。

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

namespace ConsoleApplication8 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MassCalculation myMassCalculation = new MassCalculation(); 
      TwoMassCalculation myTwoMassCalculation = new TwoMassCalculation(); 


      Console.WriteLine("How many elements are in the compound?"); 
      string userMainInput = Console.ReadLine(); 


      if (userMainInput == "1") 
      { 
       myMassCalculation.Amount1 = 1; 

       Console.WriteLine("What is the ELEMENT?"); 

       string userInput1 = Console.ReadLine(); 

       Elements element; 

       if (Enum.TryParse<Elements>(userInput1, true, out element)) 
       { 
        switch (element) 
        { 
         case Elements.Na: 
          myMassCalculation.Element1 = 22.990; 
          break; 
         case Elements.Cl: 
          myMassCalculation.Element1 = 35.453; 
          break; 
         default: 
          break; 
        } 
       } 
       Console.WriteLine("How many?"); 

       string userAmount1 = Console.ReadLine(); 
       int myAmount1 = int.Parse(userAmount1); 
       myMassCalculation.Amount1 = myAmount1; 

       myMassCalculation.DoCalculation(); 

       resultOfMassCalculation(myMassCalculation); 
      } 

      if (userMainInput == "2") 
      { 
       Console.WriteLine("What is the First ELEMENT?"); 

       string userInput1 = Console.ReadLine(); 

       Elements element; 

       if (Enum.TryParse<Elements>(userInput1, true, out element)) 
       { 
        switch (element) 
        { 
         case Elements.Na: 
          myMassCalculation.Element1 = 22.990; 
          break; 
         case Elements.Cl: 
          myMassCalculation.Element1 = 35.453; 
          break; 
         default: 
          break; 
        } 
       } 

       Console.WriteLine("How many?"); 

       string userAmount1 = Console.ReadLine(); 
       int myAmount1 = int.Parse(userAmount1); 
       myMassCalculation.Amount1 = myAmount1; 


       Console.WriteLine("What is the Second ELEMENT?"); 

       string userInput2 = Console.ReadLine(); 

       if (Enum.TryParse<Elements>(userInput2, true, out element)) 
       { 
        switch (element) 
        { 
         case Elements.Na: 
          myTwoMassCalculation.Element2 = 22.990; 
          break; 
         case Elements.Cl: 
          myTwoMassCalculation.Element2 = 35.453; 
          break; 
         default: 
          break; 
        } 
       } 

       Console.WriteLine("How many?"); 

       string userAmount2 = Console.ReadLine(); 
       int myAmount2 = int.Parse(userAmount2); 
       myTwoMassCalculation.Amount2 = myAmount2; 

       myTwoMassCalculation.DoCalculation(); 

       resultOfMassCalculation(myTwoMassCalculation); 
      } 


      Console.ReadLine(); 
     } 
     private static void resultOfMassCalculation(MassCalculation calculation) 
     { 
      Console.Write("The Mass is {0}g/mol", calculation.DoCalculation()); 
     } 
    } 
    enum Elements 
    { 
     Na, 
     Cl, 
    } 



    class MassCalculation 
    { 
     public double Element1 { get; set; } 
     public int Amount1 { get; set; } 

     public virtual double DoCalculation() 
     { 
      double result = Element1 * Amount1; 
      return result; 
     } 
    } 
    class TwoMassCalculation : MassCalculation 
    { 
     public double Element2 { get; set; } 
     public int Amount2 { get; set; } 

     public override double DoCalculation() 
     { 
      double result = Element1 * Amount1 + Element2 * Amount2; 
      return result; 
     } 
    } 
} 

請幫忙!我知道這似乎有些不專業。我剛剛開始編程一週前,這是我能做的最好的。我需要指導。

在代碼中定義的唯一元素是Na和Cl,我試圖計算NaCl。當一切都到位時,我會在列表中添加更多元素,以及更多不同類型的計算。

我會採取建設性的意見。

非常感謝你提前。

+0

你得到一個錯誤,或者僅僅是excpected結果不好嗎? – Vajura

+1

你的意思是「不正常工作」是什麼意思? – TJennings

+0

它可以工作,但每次我嘗試計算2個元素化合物,例如NaCl時,代碼忽略第1個元素,並且只計算第2個元素,這就是 –

回答

1

當元素爲兩個時,代碼中存在問題。您將第一個元素值分配給「myMassCalculation」對象,將第二個元素值分配給「myTwoMassCalculation」。當您調用「DoCalculation()」時,「myTwoMassCalculation.Element1」和「myTwoMassCalculation.Amount1」沒有值。這就是爲什麼它給出了錯誤的答案。做以下修改和嘗試:

  if (Enum.TryParse<Elements>(userInput1, true, out element)) 
      { 
       switch (element) 
       { 
        case Elements.Na: 
         myTwoMassCalculation.Element1 = 22.990; 
         break; 
        case Elements.Cl: 
         myTwoMassCalculation.Element1 = 35.453; 
         break; 
        default: 
         break; 
       } 
      } 

      Console.WriteLine("How many?"); 

      string userAmount1 = Console.ReadLine(); 
      int myAmount1 = int.Parse(userAmount1); 
      myTwoMassCalculation.Amount1 = myAmount1; 
+0

我試着讓「TwoMassCalculation」這個類從「MassCalculation」派生出來,我假設「TwoMassCalculation」類繼承了「MassCalculation」的每一個特徵,你可以在第141行看到我的觀點:class TwoMassCalculation:MassCalculation。你可以說我試圖避免「複製粘貼」。謝謝你的建議。 –

1

我會做這樣的事情:

  1. 創建要素類(名稱(字符串),無論這個數字是(雙/十進制)

  2. 創建他們它們中的一個靜態辭典按名稱索引。

  3. 遍歷查找每個E的參數的主要(或周圍的輸入命令迴路)然後在字典中進行計算。

  4. 如果需要轉換爲LINQ。

這是一個很好的方法,應該教你很多。我不會爲你寫(時間和願望),但我可能會稍後再以一個例子回來。

+0

我感謝你的建議。你的想法對我的編碼技巧可以是一個很大的改進。目前,我不處於操縱數組,集合,字典和LINQ的級別。我會注意到這些建議,並將我的想法付諸積極思考。 –

2

我重構了你的代碼。它將同樣的方式工作,但不會崩潰不適當的用戶輸入

https://dotnetfiddle.net/CMQugr

using System; 
using System.Collections.Generic; 

namespace Test 
{ 
    public class Program 
    { 
     public static Dictionary<string, double> Elements = new Dictionary<string, double> 
     { 
      {"Na",22.990}, 
      {"Cl",35.453} 
     }; 

     public static void Main() 
     { 
      double result = 0; 

      int elemenCountInput; 
      do 
      { 
       Console.WriteLine("How many elements are in the compound?"); 
      } while (!Int32.TryParse(Console.ReadLine(), out elemenCountInput)); 

      for (int i = 0; i < elemenCountInput; i++) 
      { 
       string element; 
       do 
       { 
        Console.WriteLine("What is the {0} element", (i + 1)); 
        element = Console.ReadLine(); 
       } while (!Elements.ContainsKey(element)); 

       int amount; 
       do 
       { 
        Console.WriteLine("How many"); 
       } while (!Int32.TryParse(Console.ReadLine(), out amount)); 

       result += Elements[element] * amount; 
      } 

      Console.Write("The Mass is {0}g/mol", result); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

你的代碼讓我大開眼界。當我足夠了解它時,我會把你的代碼當作一個學習的例子。非常感謝! –

+0

儘管我很欣賞您爲創建完美工作代碼所做的努力,但我仍在尋找我的代碼無法按照我希望的方式工作的原因。我想採取嬰兒的步驟,走上頂峯。 –

+0

@EricDuong另一個傢伙已經發布了答案,爲什麼它不起作用。我不想重複發表。快樂學習! – Szer

相關問題