2017-10-19 6 views
1

私のキャラクタの移動のためのコードを作成していましたが、最初のポイントと2番目のポイントの間の距離と角度を探していました。私は希望の結果を得るために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 .

はここに私のコードです。しかし、コード(Visual Studioを使用)を実行した後、私はこのエラーを得ました。

+3

例えば

N3を使用する必要があります。これは、 'Main'メソッドでは2行になる可能性が高いようです:' 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は浮動小数点では使用できません。あなたは[MCVE]にこれを削減してください

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

+ codeulikeありがとうございました。 –

関連する問題