2016-07-24 91 views
0

我知道如果我只是使用连续数字,会更容易,但我想让用户更容易选择与他们选择的模具类型相对应的数字。如果我使用操作符或操作符,我仅限于比较两件事情?这是我试图做的,但没有奏效。是我的语法错误或我不能串在一条语句?:如何从操作中排除多个值?我会使用一个or运算符以及if else语句吗?

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; 
     } 

    } 
+0

您是否想要评论部分比较? 'typeOfDice!= // 10 ...'这行可能不会按原样编译。 –

回答

1

你想要一个& &(逻辑与)测试之间不是|| (逻辑OR)

if (typeOfDice != 4 && 
    typeOfDice != 6 && 
    typeOfDice != 8 && 
    typeOfDice != 10 && 
    typeOfDice != 12 && 
    typeOfDice != 20 && 
    typeOfDice != 100) 

随着|| operator,如果你还选择骰子正确的值,如果在评估始终是真实的。
例如,假设你的用户选择一个骰子= 4,现在这个骰子值(4)不同于骰子值6(或其他可接受的值),因此你的条件将总是找到一个真实的条件,你的代码打印错误消息

另外我想向您介绍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语句代码的变量。代替这个着名的错误做法,我在代码中添加了一个无限循环。菜单中的新选项(退出)允许用户退出循环并终止程序

+0

这似乎已经修复了,谢谢。但是我现在正在为goto Start收到一个错误;声明在else语句之前。它想要一个大括号,但我认为我可以在那里有一个分号,它会把它带回顶部的开始标签。在节目结束时,我也有一个开始;声明似乎工作正常。添加大括号似乎没有帮助。 – JohnnyRelentless

+0

不要使用goto。这实际上是一种不好的做法,可能是发现错误非常困难的根源。而是将所有需要在无限循环内重复的代码封装在_while(true)_中,为菜单添加一个新选项(“键入quit退出”),在进入DiceType代码解析之前检查该单词并添加一个_break_语句从无限循环中退出 – Steve