如何防止由于 C# 中的无效输入而导致崩溃?

How do I prevent crashing due to invalid input in C#?(如何防止由于 C# 中的无效输入而导致崩溃?)
本文介绍了如何防止由于 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.

我想做的是:

  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;使用 System.Text;

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

命名空间整体计算器{

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("
The 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# 中的无效输入而导致崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)