<tfoot id='jb3qC'></tfoot>

      <legend id='jb3qC'><style id='jb3qC'><dir id='jb3qC'><q id='jb3qC'></q></dir></style></legend>
    1. <i id='jb3qC'><tr id='jb3qC'><dt id='jb3qC'><q id='jb3qC'><span id='jb3qC'><b id='jb3qC'><form id='jb3qC'><ins id='jb3qC'></ins><ul id='jb3qC'></ul><sub id='jb3qC'></sub></form><legend id='jb3qC'></legend><bdo id='jb3qC'><pre id='jb3qC'><center id='jb3qC'></center></pre></bdo></b><th id='jb3qC'></th></span></q></dt></tr></i><div id='jb3qC'><tfoot id='jb3qC'></tfoot><dl id='jb3qC'><fieldset id='jb3qC'></fieldset></dl></div>
      • <bdo id='jb3qC'></bdo><ul id='jb3qC'></ul>
    2. <small id='jb3qC'></small><noframes id='jb3qC'>

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

      时间:2023-05-21

      <small id='xUYea'></small><noframes id='xUYea'>

        <tbody id='xUYea'></tbody>
      <i id='xUYea'><tr id='xUYea'><dt id='xUYea'><q id='xUYea'><span id='xUYea'><b id='xUYea'><form id='xUYea'><ins id='xUYea'></ins><ul id='xUYea'></ul><sub id='xUYea'></sub></form><legend id='xUYea'></legend><bdo id='xUYea'><pre id='xUYea'><center id='xUYea'></center></pre></bdo></b><th id='xUYea'></th></span></q></dt></tr></i><div id='xUYea'><tfoot id='xUYea'></tfoot><dl id='xUYea'><fieldset id='xUYea'></fieldset></dl></div>
    3. <tfoot id='xUYea'></tfoot>

            <legend id='xUYea'><style id='xUYea'><dir id='xUYea'><q id='xUYea'></q></dir></style></legend>
            • <bdo id='xUYea'></bdo><ul id='xUYea'></ul>

                本文介绍了如何防止由于 C# 中的无效输入而导致崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我编写的程序设置为只接受正整数作为输入.如果用户输入了一个字母,那么它就会崩溃.负整数不会引起任何问题,尽管就我的程序如何运行而言它不是有效的".

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

                上一篇:如何分析 Windows Crash Reporter 生成的 WERInternalMetadata.xml 文件? 下一篇:如何对事件日志中的 .NET 2.0 错误报告消息进行故障排除?

                相关文章

                  <i id='x6soG'><tr id='x6soG'><dt id='x6soG'><q id='x6soG'><span id='x6soG'><b id='x6soG'><form id='x6soG'><ins id='x6soG'></ins><ul id='x6soG'></ul><sub id='x6soG'></sub></form><legend id='x6soG'></legend><bdo id='x6soG'><pre id='x6soG'><center id='x6soG'></center></pre></bdo></b><th id='x6soG'></th></span></q></dt></tr></i><div id='x6soG'><tfoot id='x6soG'></tfoot><dl id='x6soG'><fieldset id='x6soG'></fieldset></dl></div>

                  1. <small id='x6soG'></small><noframes id='x6soG'>

                    <legend id='x6soG'><style id='x6soG'><dir id='x6soG'><q id='x6soG'></q></dir></style></legend>
                  2. <tfoot id='x6soG'></tfoot>
                      <bdo id='x6soG'></bdo><ul id='x6soG'></ul>