VS Studio终端无法启动,程序无法启动

问题描述 投票:-2回答:1

我正在尝试运行我在MS Visual Studio(mac)enter image description here中编写的程序,当我在没有调试的情况下启动程序时,终端将无法启动,程序将无法启动。 VS内部弹出一个内部终端,并显示以下消息:

未处理的异常:System.NullReferenceException:未将对象引用设置为对象的实例。在/ Users / brettcohen / Desktop / MIS 333K / Cohen_Brett_HW1_WORKING / Cohen_Brett_HW1_WORKING / Program.cs中的Cohen_Brett_HW1.Program.CheckCode(String strInput):/ Users / brettcohen / Desktop中Cohen_Brett_HW1.Program.Main(String [] args)的第122行/ MIS 333K / Cohen_Brett_HW1_WORKING / Cohen_Brett_HW1_WORKING / Program.cs:第41行

我的代码看起来像这样:

using System;

namespace Cohen_Brett_HW1
{
    class Program
    {
        //Named constants
        public const decimal TacoPrice = 2.00m;
        public const decimal SandwichPrice = 7.00m;
        public const decimal TaxRate = .0825m;


        static void Main(string[] args)
        {
            //Input variables
            String strCustCodeInput;
            int intTacoCountInput;
            String strTacoCountInput;
            int intSandwichCountInput;
            String strSandwichCountInput;


            //Variable for customer code validity
            Boolean boolCodeValid;


            //Get the customer code
            do
            {
                //Request input
                Console.WriteLine("Please enter the customer code:");
                strCustCodeInput = Console.ReadLine();

                //Make sure the code is valid
                boolCodeValid = CheckCode(strCustCodeInput);

            } while (boolCodeValid == false); //loop if customer code is invalid


            do
            {
                //Get the taco order count
                do
                {
                    //Request user input
                    Console.WriteLine("Enter the number of tacos you would like to order:");

                    //Read the input
                    strTacoCountInput = Console.ReadLine();

                    //Pass the string to CheckItem method and assign the result to intTacoCountInput
                    intTacoCountInput = CheckItem(strTacoCountInput);
                } while (intTacoCountInput == -1); //Loop if they enter an invalid number


                //Get the sandwich order count
                do
                {   //Request user input
                    Console.WriteLine("Enter the number of sandwiches you would like to order:");

                    //Read the input
                    strSandwichCountInput = Console.ReadLine();

                    //Pass the string to CheckItem method and assign the result to intTacoCountInput
                    intSandwichCountInput = CheckItem(strSandwichCountInput);

                } while (intSandwichCountInput == -1); //Loop if they enter an invalid number

                if (intTacoCountInput + intSandwichCountInput == 0)
                {
                    Console.WriteLine("Total number of items ordered must be greater than 0." + "\n");

                }
            } while (intTacoCountInput + intSandwichCountInput < 1); //make sure that total items ordered is at least one


            //Output variables

            //Total item count
            int intTotalItemCount = intTacoCountInput + intSandwichCountInput;

            //Taco subtotal
            decimal TacoSubtotal = TacoPrice * intTacoCountInput;

            //Sandwich subtotal
            decimal SandwichSubtotal = SandwichPrice * intSandwichCountInput;

            //Combined Subtotal
            decimal CombinedSubtotal = TacoSubtotal + SandwichSubtotal;

            //Tax Amount
            decimal TaxAmount = CombinedSubtotal * TaxRate;

            //Grand Total
            decimal GrandTotal = CombinedSubtotal + TaxAmount;

            //Console output
            Console.WriteLine("Customer Code: " + strCustCodeInput.ToUpper());
            Console.WriteLine("Total Items: " + intTotalItemCount);
            Console.WriteLine("Taco Subtotal: " + (TacoSubtotal.ToString("C")));
            Console.WriteLine("Sandwich Subtotal: " + (SandwichSubtotal.ToString("C")));
            Console.WriteLine("Full Subtotal: " + (CombinedSubtotal.ToString("C")));
            Console.WriteLine("Sales Tax: " + (TaxAmount.ToString("C")));
            Console.WriteLine("Grand Total: " + (GrandTotal.ToString("C")));

            //Keep console open
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();

        }


        //This method validates the customer code
        public static Boolean CheckCode(String strInput)
        {
            if (strInput.Length < 4 || strInput.Length > 6)
            {
                Console.Write("Please enter a valid code, 4 to 6 characters, letters only." + "\n");
                return false;
            }

            else
            {   //make sure that input is letters only
                foreach (char c in strInput)
                {
                    if (!char.IsLetter(c))
                    {
                        Console.WriteLine("Customer code must consist of letters only." + "\n");
                        return false;
                    }

                    else
                    {
                        return true;
                    }
                }
                return true;
            }
        }


        //This method validates the customer selection
        static int CheckItem(String strInput)
        {
            //Establish integer variable
            int NowInt;

            //Convert the strInput to an integer
            NowInt = Convert.ToInt32(strInput);

            //Validate
            if (NowInt > -1)
            {
                return NowInt;
            }
            else
            {
                Console.Write("Please enter a valid number greater than or equal to 0." + "\n");
                return -1;
            }
        }
    }
}

在此先感谢您的帮助!

c# terminal
1个回答
0
投票

使用.NET Fiddle我能够运行你的程序只需将public放在你的方法static void Main(string[] args)之前和class Program之前。我也改变了你的CheckCode方法,因为没有适当的验证来检查是否只有字母。

按照提供的链接检查完整的代码,否则只需在我告诉你的地方添加public,添加这个库using System.Text.RegularExpressions;并修改你的CheckCode方法来匹配这个:

public static Boolean CheckCode(String strInput)
{
    if (strInput.Length < 4 || strInput.Length > 6)
    {
        Console.Write("Please enter a valid code, 4 to 6 characters, letters only." + "\n");
        return false;
    }
    else
    {   //make sure that input is letters only
        return Regex.IsMatch(strInput, @"^[a-zA-Z]+$");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.