所以我试图制作一个脚本,用户输入他们需要行驶的里程数和他们旅行的每小时英里数,然后脚本输出他们必须经过的剩余时间和小时数。我一直试图用%来找到行进里程的剩余部分/ MPH,但输出错误的数字。无论如何只能从两个分数中得到小数?例如,如果我做100/65我得到大约1.538的输出,我想只使用0.538。但是当我使用100%65时,我得到35.这是我当前的脚本供参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TimeCalculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Travel Time Calculator");
string answer;
//creates variable for the answer
do
//creates loop to continue the application
{
string grade;
//Console.WriteLine(100-(int)((double)100/65)*65);
Console.WriteLine(" ");
Console.Write("Enter miles: ");
Decimal val1 = Convert.ToDecimal(Console.ReadLine());
//converts input to decimal allowing user to use decimals
Console.Write("Enter miles per hour: ");
Decimal val2 = Convert.ToDecimal(Console.ReadLine());
//converts input to decimal allowing user to use decimals
Console.WriteLine(" ");
Console.WriteLine("Estimated travel time");
Console.WriteLine("Hours: " + (((int)val1 / (int)val2)));
//converts values to integers and divides them to give hours traveled
//double floor1 = Math.Floor(((double)val1/(double)val2));
Console.WriteLine("Minutes: " + (Decimal.Remainder((decimal)val1, (decimal)val2)));
//converts values to double and gets the remainder of dividing both values to find minutes
Console.WriteLine();
//enters one line
Console.Write("Continue? (y/n): ");
answer = Console.ReadLine();
//the string is equal to what the user inputs
Console.WriteLine();
}
while (answer.ToUpper() == "Y");
//if y, the application continues
}
}
答案 0 :(得分:4)
答案 1 :(得分:0)
如果你想要从初始值开始的小时和分钟,那么这应该为你做(你可能需要一些明确的演员,这是未经测试的)
var result = va1 / val2;
var hours = Math.Floor(result);
var minutes = (result - hours) * 60;