怎样防止由于在C#中轰然无效的输入? [英] How do I prevent crashing due to invalid input in C#?

查看:89
本文介绍了怎样防止由于在C#中轰然无效的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写程序设置为只接受正整数作为输入。如果用户输入一个字母,则说明崩溃。负整数不会引起任何问题,虽然它不是关于有效,以

The program I've written is set to only accept positive integers as input. If the user inputs a letter instead, then it crashes. Negative integers don't cause any problems, though it's not 'valid' in regards to how my program functions.

我想要做的怎么样我的程序的功能是:

What I want to do is:


  1. 从无效的输入崩溃阻止程序。

  1. Prevent the program from crashing from invalid input.

显示错误消息,如果输入无效

Display an error message if the input is invalid

让程序继续停止的地方,而不影响程序的其余部分。

Have the program continue where it left off, without affecting the rest of the program.

另外,我计划的一部分,涉及到部门。 ?有没有一种方法,以防止进入全零用户

Also, a part of my program involves division. Is there a way to prevent the user from entering all zeros?

这是在C#

我的代码:

using System;



使用System.Collections.Generic;
使用System.Linq的;

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

命名空间OverallCalculator
{

namespace OverallCalculator {

class Program
{
    static void Main(string[] args)
    {
        bool shouldContinue;


        do
        {




            Console.WriteLine("Enter Striking Level: ");

            string striking = Console.ReadLine();

            Console.WriteLine("Enter Grappling Level: ");

            string grappling = Console.ReadLine();

            Console.WriteLine("Enter Submission Level: ");

            string submission = Console.ReadLine();

            Console.WriteLine("Enter Durability Level: ");

            string durability = Console.ReadLine();

            Console.WriteLine("Enter Technical Level: ");

            string technical = Console.ReadLine();

            Console.WriteLine("Enter Speed Level: ");

            string speed = Console.ReadLine();

            Console.WriteLine("Enter Hardcore Level: ");

            string hardcore = Console.ReadLine();

            Console.WriteLine("Enter Charisma Level: ");

            string charisma = Console.ReadLine();




            int gra = Convert.ToInt32(grappling);
            int str = Convert.ToInt32(striking);
            int dur = Convert.ToInt32(durability);
            int spd = Convert.ToInt32(speed);
            int tec = Convert.ToInt32(technical);
            int hdc = Convert.ToInt32(hardcore);
            int cha = Convert.ToInt32(charisma);
            int sub = Convert.ToInt32(submission);

            int total = str + gra + sub + dur + tec + spd + cha + hdc;


            int overall = total / 8 + 8;




            Console.WriteLine("The Overall is " + overall);
            Console.WriteLine("Do you wish to continue? y/n? ");



            if (Console.ReadLine() == "y")
            {
                shouldContinue = true;


            }
            else break;


        } while (shouldContinue == true);
    }
}



}

}

推荐答案

在这里你去:

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

namespace OverallCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            bool shouldContinue = true;

            while (shouldContinue)
            {
                int strikingLevel = GetValue("Enter Striking Level: ");
                int grapplingLevel = GetValue("Enter Grappling Level: ");
                int submissionLevel = GetValue("Enter Submission Level: ");
                int durabilityLevel = GetValue("Enter Durability Level: ");
                int technicalLevel = GetValue("Enter Technical Level: ");
                int speedLevel = GetValue("Enter Speed Level: ");
                int hardcoreLevel = GetValue("Enter Hardcore Level: ");
                int charismaLevel = GetValue("Enter Charisma Level: ");

                int total = strikingLevel + grapplingLevel + durabilityLevel + submissionLevel +
                    technicalLevel + speedLevel + charismaLevel + hardcoreLevel;

                int overall = total / 8 + 8;

                Console.WriteLine("\nThe Overall is {0}.", overall);
                while (true)
                {
                    Console.WriteLine("Do you wish to continue? y/n? ");
                    string response = Console.ReadLine();
                    if (response.Equals("y", StringComparison.CurrentCultureIgnoreCase) ||
                        response.Equals("yes", StringComparison.CurrentCultureIgnoreCase))
                    {
                        shouldContinue = true;
                        break;
                    }
                    else if (response.Equals("n", StringComparison.CurrentCultureIgnoreCase) ||
                        response.Equals("no", StringComparison.CurrentCultureIgnoreCase))
                    {
                        shouldContinue = false;
                        break;
                    }
                }
            } 
        }

        private static int GetValue(string prompt)
        {
            while (true)
            {
                Console.WriteLine(prompt);
                string input = Console.ReadLine();
                int value;
                if (int.TryParse(input, out value))
                {
                    if (value <= 0)
                        Console.WriteLine("Please enter a positive number.");
                    else
                        return value;
                }
                else
                {
                    Console.WriteLine("Please enter a number.");
                }
            }
        }
    }
}

这篇关于怎样防止由于在C#中轰然无效的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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