只允许数字插入和放大器;变换小时,分钟计算黄金/分钟 - 更新 [英] Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED

查看:142
本文介绍了只允许数字插入和放大器;变换小时,分钟计算黄金/分钟 - 更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算每个养殖分钟你的黄金的应用程序。
我想打一个错误报告,也不会允许你插入在控制台输入其中,你要跟HOULD插入(如下代码所示)的时间和黄金的任何文本和显示一些错误消息,如果你这样做,让您重新插入(某种循环或者如果/其他的事情......)
希望我做我自己清楚,你我是新来这...所以我希望你明白。
这是我到目前为止的代码:
------------------------------- //// //////的问题

更新,因为我不想做出相同的代码一个新的问题:

我如何在这段代码中我的时间转换成分钟,1.2小时内浮子计算将计算到72分钟不是80(我已经把下面的注释代码是哪里的问题)

I have an application that calculates your gold per minute farmed. I want to make an error report that wouldn't allow you to insert any text in the console input where yous hould insert the time and gold(as shown in code below) and show some error message if you do and let you redo the insertion (some sort of loop or if/else thing ...) Hope i made my self clear,thou I'm new to this... so i hope you understand. Here is my code so far : -------------------------------//////////
Update on question because i didn't want to make a new question for the same code :
How do i transform in this code my hours into minutes,the float calculation of 1.2 hours will calculate into 72 minutes not 80.(i've put comment below in code where the problem is)

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.Parse ,您可以使用< A HREF =htt​​p://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx> int.TryParse ,并打印一则消息:

Instead of using int.Parse, you can use int.TryParse, and print a message:

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

这可以让你重新提示,正确处理错误。

This allows you to re-prompt and handle errors correctly.

这篇关于只允许数字插入和放大器;变换小时,分钟计算黄金/分钟 - 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆