运行 我在 C# 中应用十进制格式后的代码时出错
Error while running my code in C# after applying a decimal format
我正在为我的角色的移动创建一个代码(为了我的任务),并想找出第一个点和第二个点之间的距离和角度。我使用 Math
class 和 ToString
方法来获得所需的结果。但是在 运行 代码之后(使用 visual studio)我得到了这个错误:
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
.
这是我的代码:
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
感谢您的帮助。
格式 D3 不能与浮点数一起使用。你需要使用 n3
例如
Console.WriteLine(dist12.ToString("n3"));
我正在为我的角色的移动创建一个代码(为了我的任务),并想找出第一个点和第二个点之间的距离和角度。我使用 Math
class 和 ToString
方法来获得所需的结果。但是在 运行 代码之后(使用 visual studio)我得到了这个错误:
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 .
这是我的代码:
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
感谢您的帮助。
格式 D3 不能与浮点数一起使用。你需要使用 n3
例如
Console.WriteLine(dist12.ToString("n3"));