2014-10-05 2 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

죄송합니다.이 사이트의 헌장은 가축으로 이어지지 않습니다. –

+0

** 무엇이 잘못되었는지를 알려주는 ** 오류 메시지를 읽으십시오. – SLaks

답변

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

형식 문자열이 존재하지 않는 인수 {3}을 의미 {2}로 교체.

+0

고마워요.하지만 대답은 항상 "y = 0x"입니다. 이유를 설명해 주실 수 있습니까? – Piggy

+0

@Piggy'int answer = -1/numVal;'이 옳지 않습니다. 'double answer = -1.0/numVal;'('1.0'에주의하십시오)와 같아야합니다. 정수 부분은 분수 부분을 잘라냅니다. – AlexD

+0

감사합니다. -1이 잘못되었음을 깨달았지만 지금은 변경하지 않았습니다. – Piggy

관련 문제