gpt4 book ai didi

c# - 只允许插入数字并将小时数转换为分钟数以计算黄金/分钟 - 已更新

转载 作者:行者123 更新时间:2023-11-30 19:27:21 25 4
gpt4 key购买 nike

我有一个应用程序可以计算您每分钟耕种的黄金。我想制作一个错误报告,不允许您在控制台输入中插入任何文本,您应该在其中插入时间和黄金(如下面的代码所示)并在您这样做时显示一些错误消息并让您重做插入(某种循环或 if/else ……)希望我说清楚了,你是新来的......所以我希望你能理解。到目前为止,这是我的代码:------------------------------//////////
更新问题,因为我不想为相同的代码提出新问题:
我如何在这段代码中将我的小时数转换为分钟,1.2 小时的浮点计算将计算为 72 分钟而不是 80 分钟。(我在问题所在的代码中添加了注释)

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

namespace YourGold
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to YourGold App! \n------------------------");
Console.WriteLine("Inesrt your gold: ");
int gold = int.Parse(Console.ReadLine());
Console.WriteLine("Your gold is : " + gold);
Console.WriteLine("Inesrt your time(In Hours) played: ");
float hours = float.Parse(Console.ReadLine());
int minutes = 60;
float time = (float)hours * minutes; // Here the calculation are wrong...
Console.WriteLine("Your total time playd is : " + time + " minutes");
float goldMin = gold / time;
Console.WriteLine("Your gold per minute is : " + goldMin);
Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");
Console.ReadLine();

}
}
}

谢谢!

最佳答案

您可以使用 int.TryParse 而不是使用 int.Parse , 并打印一条消息:

int gold;
while(!int.TryParse(Console.ReadLine(), out gold))
{
Console.WriteLine("Please enter a valid number for gold.");
Console.WriteLine("Inesrt your gold: ");
}

这允许您重新提示并正确处理错误。

关于c# - 只允许插入数字并将小时数转换为分钟数以计算黄金/分钟 - 已更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18880599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com