Je sais que ce serait plus simple si j'utilisais simplement des nombres consécutifs, mais je veux que l'utilisateur choisisse plus facilement un nombre correspondant au type de matrice qu'il choisit. Si j'utilise un opérateur ou suis-je limité à comparer seulement deux choses? C'est ce que j'ai essayé de faire mais ça n'a pas marché. Est-ce ma syntaxe incorrecte ou puis-je pas plusieurs termes chaîne dans une déclaration ?:Comment puis-je exclure plusieurs valeurs d'une opération? Aurais-je utiliser un opérateur ou avec une instruction if else?
if (typeOfDice != 4 || typeOfDice != 6 ||
typeOfDice != 8 || typeOfDice != //10 || typeOfDice != 12 ||
typeOfDice != 20 || typeOfDice != 100)
Voici le programme court, je suis en train de le faire fonctionner. Je veux juste vous assurer que l'utilisateur peut » t briser le programme:
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;
}
}
Vouliez-vous dire une partie de la comparaison? 'typeOfDice! = // 10 ...' Cette ligne ne sera probablement pas compilée telle quelle. –