[英]How can I exclude multiple values from an operation? Would I use an or operator along with an if else statement?
我知道如果只使用连续数字会更容易,但是我想使用户更容易选择与他们选择的模具类型相对应的数字。 如果使用or运算符,我是否只能比较两件事? 这是我试图做的,但是没有用。 我的语法错误吗?或者我不能在一个语句中将多个术语字符串化吗?:
if (typeOfDice != 4 || typeOfDice != 6 ||
typeOfDice != 8 || typeOfDice != //10 || typeOfDice != 12 ||
typeOfDice != 20 || typeOfDice != 100)
这是我要使其运行的简短程序。我只想确保用户不会破坏该程序:
static void Main(string[] args)
{
Start:
Random roll = new Random();
// Request dice type from user
Console.WriteLine("Please input the type of dice you want to roll. ");
// Display dice types to user
Console.WriteLine("4) Four-sided");
Console.WriteLine("6) Six-sided");
Console.WriteLine("8) Eight-sided");
Console.WriteLine("10) Ten-sided");
Console.WriteLine("12) Twelve-sided");
Console.WriteLine("20) Twenty-sided");
Console.WriteLine("100) Percentage");
Console.WriteLine(" ");
// Take dice type from user
Console.Write("Type of Dice: ");
int typeOfDice = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" ");
// Prevents user from breaking the program by printing a corrective message
// and restarting the program in the event an inappropriate choice is made by the user.
if (typeOfDice != 4 || typeOfDice != 6 ||
typeOfDice != 8 || typeOfDice != //10 || typeOfDice != 12 ||
typeOfDice != 20 || typeOfDice != 100)
Console.WriteLine("That is not an acceptable die type. Please try again.");
goto Start;
else
{
// Initiates random variable and total variable
Random rnd = new Random();
int total = 0;
// Request number of dice from user
Console.WriteLine("Please input the number of dice you want to roll ");
Console.WriteLine(" ");
// Accept number of dice from user
Console.Write("Number of Dice: ");
int numberOfDice = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" ");
/// Assigns random generator parameters to user's choice of dice type and
// generates random number, looping until the die is rolled the requested
// number of times and the result of each new roll is added to the total
switch (typeOfDice)
{
case 4:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
//Console.WriteLine(" ");
}
break;
case 6:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
//Console.WriteLine(" ");
}
break;
case 8:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
//Console.WriteLine(" ");
}
break;
case 10:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
//Console.WriteLine(" ");
}
break;
case 12:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
//Console.WriteLine(" ");
}
break;
case 20:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
// Console.WriteLine(" ");
}
break;
case 100:
for (int count = 0; count < numberOfDice; count++)
{
int currentRoll = rnd.Next(typeOfDice);
total += currentRoll + 1;
Console.Write("{0} ", currentRoll + 1);
//Console.WriteLine(" ");
}
break;
}
// Prints total of dice rolls.
Console.WriteLine(" ");
Console.WriteLine("Total: {0}", total);
Console.WriteLine(" ");
goto Start;
}
}
您希望测试之间使用&&(逻辑AND)而不是|| (逻辑或)
if (typeOfDice != 4 &&
typeOfDice != 6 &&
typeOfDice != 8 &&
typeOfDice != 10 &&
typeOfDice != 12 &&
typeOfDice != 20 &&
typeOfDice != 100)
与|| operator
|| operator
,如果您为骰子选择了正确的值,则if评估始终为true。
例如,假设您的用户选择一个骰子= 4,现在此骰子值(4)与骰子值6(或其他可接受的值)不同,因此您的条件将始终找到一个为true的条件,并且您的代码将输出错误信息
我也想向您介绍enum关键字。
public enum DiceType
{
FourSides = 4,
SixSides= 6,
EightSides = 8,
TenSides = 10,
TwelveSides = 12,
TwentySides = 20,
Percentage = 100
}
现在,您可以使用此枚举类型的变量,而不是使用幻数
Random roll = new Random();
while(true)
{
// Request dice type from user
Console.WriteLine("Please input the type of dice you want to roll. ");
// Display dice types to user
Console.WriteLine("4) Four-sided");
Console.WriteLine("6) Six-sided");
Console.WriteLine("8) Eight-sided");
Console.WriteLine("10) Ten-sided");
Console.WriteLine("12) Twelve-sided");
Console.WriteLine("20) Twenty-sided");
Console.WriteLine("100) Percentage");
Console.WriteLine(" ");
Console.WriteLine("Type quit to exit ");
// Take dice type from user
Console.Write("Type of Dice: ");
string input = Console.ReadLine();
if(input == "quit")
break;
DiceType typeOfDice;
// Try to parse the input and check if it could be an enum of the
// desidered type (Note user can also input "foursides" not just 4
if (!Enum.TryParse<DiceType>(input, true, out typeOfDice) ||
!Enum.IsDefined(typeof(DiceType), typeOfDice))
Console.WriteLine("That is not an acceptable die type. Please try again.");
else
{
....
switch (typeOfDice)
{
case DiceType.FourSides:
.....
break;
case DiceType.SixSides:
.....
break;
....
}
}
}
我还删除了goto代码。 我没有在代码中添加无限循环,而没有采用这种著名的坏习惯。 菜单中的新选项(退出)允许您的用户退出循环并终止程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.