2017-10-19 2 views
1

내 캐릭터의 움직임에 대한 코드를 만들고 첫 번째 점과 두 번째 점 사이의 거리와 각도를 찾고 싶습니다. 원하는 결과를 얻으려면 Math 클래스와 ToString 메서드를 사용했습니다. 어떤 도움십진수 형식을 적용한 후 C#에서 코드를 실행하는 동안 오류가 발생했습니다.

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

namespace PeerGradedAssignment1 
{ 
    /// <summary> 
    /// For deciding the approach of point 1 to point 2 
    /// </summary> 

    class Program 
    { /// <summary> 
     /// Taking values of points  
     /// </summary> 
     /// <param name="args">Command-line args</param> 
     static void Main(string[] args) 
     { 
      // Asking for values(X,Y) of point1 and point 

      Console.Write("Welcome"); 
      Console.WriteLine(". In this aplication I will calculate" + 
          "the distance between two pints and the amgle between them."); 
      Console.WriteLine(); 

      Console.Write("Enter the X value for the 1st point: "); 
      float pointX1 = float.Parse(Console.ReadLine()); 

      Console.Write("Enter the Y value for the 1st point: "); 
      float pointY1 = float.Parse(Console.ReadLine()); 

      Console.WriteLine(); 

      Console.Write("Enter the X value for the 2nd point: "); 
      float pointX2 = float.Parse(Console.ReadLine()); 

      Console.Write("Enter the Y value for the 2nd point: "); 
      float pointY2 = float.Parse(Console.ReadLine()); 

      // Claculating the distance between point1 and point2 

      float deltaX = pointX2 - pointX1; 
      float deltaY = pointY2 - pointY1; 
      // dist12 , 12 stands for 1-2 
      float squaredist12 = deltaX * deltaX + deltaY * deltaY; 
      float dist12 = (float)Math.Sqrt(squaredist12); 
      Console.WriteLine("Distance between point and point 2:" + " " + dist12); 

      // Calculating the angle between them 

      float radians = (float)Math.Atan2(deltaX,deltaY); 

      // Converting radians to angles 

      float degrees = (float)radians * (180/(float)Math.PI); 
      Console.WriteLine(degrees); 
      Console.WriteLine(dist12.ToString("D3")); 
     } 
    } 
} 

This is the image for result

감사 :

Unhandled Exception: System.FormatException: Format specifier was invalid. at System.Number.FormatSingle(Single value, String format, NumberFormatInfo info) at System.Single.ToString(String format) at PeerGradedAssignment1.Program.Main(String[] args) in G:\RIT\new vs coursera c3 progjects\PeerGradedAssignment1\PeerGradedAssignment1\Program.cs:line 58 .

여기 내 코드입니다 :하지만 코드 (비주얼 스튜디오를 사용)를 실행 한 후 나는이 오류가 발생했습니다.

+3

는 [mcve]이를 줄여줍니다 N3 사용해야합니다. 'Main' 메쏘드에서 쉽게 두 줄을 쓸 수있는 것 같습니다 :'float value = 123.45f; Console.WriteLine (dist.ToString ("D3")); ' –

+0

포맷 지정자가 유효하지 않다는 오류 메시지를 읽었습니다. 아마도 문서를봤을 때 https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings을 찾았을 것입니다. – Chris

+0

"D3"을 사용하여 부동 소수점을 정수 형식으로 직접 변환 할 수 없습니다. Try ((int) dist12) .ToString ("D3") – jdweng

답변

4

형식 D3은 부동 소수점과 함께 사용할 수 없습니다. 당신은

예컨대

Console.WriteLine(dist12.ToString("n3")); 
+0

+ codeulike 덕분에 많은 것에 대해 알지 못했습니다. –

관련 문제