分别计算分针和时针在表盘上所指的度数,再计算两者的差。
假设输入数据无误。
using System;
using System.Collections.Generic;
using System.Text;
namespace GetAngle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入时和分,以回车分隔");
float h = 0;
float m = 0;
string sh = Console.ReadLine();
string sm = Console.ReadLine();
float.TryParse(sh, out h); //将string类型转换为float类型
float.TryParse(sm, out m);
float angleM = m / 60 * 360;
if (h >= 12)
{
h = h - 12;
}
float angleH = h * 30 + m / 60 * 30; //时针角度计算
float angle = Math.Abs(angleH - angleM);
Console.WriteLine(angle);
Console.ReadKey();
}
}
}