2014-10-05 100 views
0
static void Main(string[] args) 
    { 
     int numVal = -1; 
     int numval = -1; 
     bool repeat = true; 

     Console.WriteLine("Welcome to the perpendicular line finder!"); 

     while (repeat == true) 
     { 
      Console.WriteLine("Please write the gradient of the line"); 


      string userValue = Console.ReadLine(); 

      try 
      { 
       numVal = Convert.ToInt32(userValue); 
      } 
      catch (FormatException e) 
      { 
       Console.WriteLine("That is not a valid number"); 
       continue; 
      } 
      catch (OverflowException e) 
      { 
       Console.WriteLine("That number is too large, sorry i cannot help you"); 
       continue; 
      } 
      Console.WriteLine("So the gradient is {0}? Y/N", numVal); 
      string go = Console.ReadLine(); 
      if (go == "Y" || go == "y") 
      { 
       repeat = true; 
      } 
      else 
      { 
       repeat = false; 
      } 

      Console.WriteLine("Please write the number that was added or subtracted"); 
      Console.WriteLine("but if it was subtracted leave the minus sign in"); 

      string userValue2 = Console.ReadLine(); 

      try 
      { 
       numval = Convert.ToInt32(userValue2); 
      } 
      catch (FormatException e) 
      { 
       Console.WriteLine("That is not a valid number"); 
       continue; 
      } 
      catch (OverflowException e) 
      { 
       Console.WriteLine("That number is too large, sorry i cannot help you"); 
       continue; 
      } 
      Console.WriteLine("So the added or subtracted number is {0}? Y/N", numval); 
      string go1 = Console.ReadLine(); 
      if (go1 == "Y" || go1 == "y") 
      { 
       repeat = true; 
      } 
      else 
      { 
       repeat = false; 
      } 

      int answer = -1/numVal; 

      Console.WriteLine("A perpendicular line to y = {0}x+{1} is y = {3}x", numVal, userValue2, answer); 
      Console.ReadLine(); 
     } 

它配備了這個錯誤: 型「System.FormatException」未處理的異常出現在mscorlib.dll 這種情況發生時我運行代碼,然後轉向代碼的倒數第二行。 我是新來的c#,不知道該怎麼做,請幫助我。「類型‘System.FormatException’未處理的異常出現在mscorlib.dll」

+1

對不起,但本網站的章程不包括牲畜。 – 2014-10-05 19:52:18

+0

**閱讀錯誤消息**,它會告訴你到底發生了什麼問題。 – SLaks 2014-10-05 19:54:31

回答

0
Console.WriteLine("A perpendicular line to y = {0}x+{1} is y = {3}x", 
    numVal, userValue2, answer); 

格式字符串是指不存在的參數{3},更換{2}

+0

謝謝,但它給出的答案始終是「y = 0x」,你能解釋爲什麼。 – Piggy 2014-10-05 20:02:49

+0

@Piggy'int answer = -1/numVal;'看起來不正確。應該是'double answer = -1.0/numVal;'(注意'1.0')。整數除法小數部分。 – AlexD 2014-10-05 20:06:35

+0

謝謝你,我意識到-1是錯誤的,但我現在沒有如何改變它。 – Piggy 2014-10-05 20:09:11

相關問題