2

ファーミングされた 1 分あたりのゴールドを計算するアプリケーションがあります。時間と金を挿入する必要があるコンソール入力にテキストを挿入することを許可しないエラーレポートを作成し(以下のコードに示すように)、挿入した場合はエラーメッセージを表示し、挿入をやり直すことができるようにしたいと考えています。 (何らかのループまたは if/else のようなもの...) 私がこれに慣れていないことを明確にしたことを願っています...だから、理解していただければ幸いです。これまでの私のコードは次のとおりです: -------------------------------//////////
Update on同じコードに対して新しい質問を作成したくなかったため、質問:
このコードで私の時間を分に変換するにはどうすればよいですか?1.2 時間のフロート計算は 80 分ではなく 72 分に計算されます。問題のあるコードで)

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();

        }
    }
}

ありがとうございました!

4

2 に答える 2

8

を使用する代わりに、 を使用してメッセージを出力int.Parseできます。int.TryParse

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

これにより、プロンプトを再表示してエラーを正しく処理できます。

于 2013-09-18T19:24:10.847 に答える