我正在尝试运行我在MS Visual Studio(mac)中编写的程序,并且当我在未调试的情况下启动程序时,终端不会启动,程序也不会启动。 VS内部弹出内部终端,并显示以下消息:
未处理的异常:System.NullReferenceException:对象引用 未设置为对象的实例。在 Cohen_Brett_HW1.Program.CheckCode(String strInput)在 /用户/布雷特科恩/桌面/ MIS 333K / Cohen_Brett_HW1_WORKING / Cohen_Brett_HW1_WORKING / Program.cs:line 122,位于Cohen_Brett_HW1.Program.Main(String [] args)中 /用户/布雷特科恩/桌面/ MIS 333K / Cohen_Brett_HW1_WORKING / Cohen_Brett_HW1_WORKING / Program.cs:line 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;
}
}
}
}
在此先感谢您的帮助!
答案 0 :(得分: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]+$");
}
}