2016-07-16 119 views
-2

我有一个任务,需要我从用户请求数组大小,然后请求数字来填充数组。该计划仅打印每个循环的唯一编号。它还会通知用户他们输入的号码是否重复。我已经完成了这个工作,它应该如此。导师随后发布了一个教程视频,介绍如何编写它。这段代码与我的完全不同,我试图重写它来理解她的逻辑。我无法按照教程显示的那样工作,而且我也不理解她所包含的一些内容。有人可以看看这个,并帮助我了解她正在尝试做什么,如果它按照书面方式工作?与教练苦苦挣扎教程

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace DuplicateHandsOn 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //create an array of type int 
      int[] aList; 

      //create a counter to keep track of how many numbers have been entered 
      int counter = 0; 

      //create a boolean flag to let us know whether the number can be added or not 
      bool isDuplicate = false; 

      //ask the user how many numbers they will be entering 
      Console.WriteLine("How many numbers will you enter?"); 
      int arraySize = Convert.ToInt32(Console.ReadLine()); 

      //initialize the array with that amount 
      aList = new int[arraySize]; 

      while (counter < arraySize) 
      { 
       //prompt the user for the first number 
       Console.Write("Enter Number: "); 
       int num1 = Convert.ToInt32(Console.ReadLine()); 

       //check if the number is between 10 and 100 
       if (num1 >= 10 || num1 <= 100) 
       { 
        //check if this number exists in the array 
        for (int i = 0; i < aList.Length; i++) 
        { 
         if (aList[i] == num1) 
         { 
          //this number exists in the list 
          Console.WriteLine("{0} has already been entered", aList[i]); 
          isDuplicate = true; 
         } 
        } 

        if (isDuplicate) 
        { 
         //put the number into the array 
         aList[counter] = num1; 
        } 

        //print the array 
        for (int j = 0; j < aList.Length; j++) 
        { 
         //exclude zeros 
         if (aList[j] == 0) 
         { 
          continue; 
         } 
         else 
         { 
          Console.WriteLine(aList[j]); 
         } 
        } 

        //increment the counter 
        counter++; 

        //reset the flag 
        isDuplicate = false; 
       } 
       else 
       { 
        Console.WriteLine("Numbers should be between 10 and 100"); 
       } 
      } 

      #if DEBUG 
      Console.ReadKey(); 
      #endif 

     } 
    } 
} 
+0

它并没有为我使用CSC.EXE C#编译器在.NET –

回答

1

代码中存在三个错误,以及很多样式问题,但是这些错误很容易指出。我添加了// FIX评论来显示我改变的内容,希望它有道理。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace DuplicateHandsOn 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //create an array of type int 
      int[] aList; 

      //create a counter to keep track of how many numbers have been entered 
      int counter = 0; 

      //create a boolean flag to let us know whether the number can be added or not 
      bool isDuplicate = false; 

      //ask the user how many numbers they will be entering 
      Console.WriteLine("How many numbers will you enter?"); 
      int arraySize = Convert.ToInt32(Console.ReadLine()); 

      //initialize the array with that amount 
      aList = new int[arraySize]; 

      while (counter < arraySize) 
      { 
       //prompt the user for the first number 
       Console.Write("Enter Number: "); 
       int num1 = Convert.ToInt32(Console.ReadLine()); 

       // FIX: This should be && instead of || to test if 
       // both of these conditions are true to match 
       // the comment 
       //check if the number is between 10 and 100 
       if (num1 >= 10 && num1 <= 100) 
       { 
        //check if this number exists in the array 
        for (int i = 0; i < aList.Length; i++) 
        { 
         if (aList[i] == num1) 
         { 
          //this number exists in the list 
          Console.WriteLine("{0} has already been entered", aList[i]); 
          isDuplicate = true; 
         } 
        } 

        // FIX: This should only happen if the number 
        // is not a duplicate 
        if (!isDuplicate) 
        { 
         //put the number into the array 
         aList[counter] = num1; 

         // FIX: Move this line into here to only increment 
         // the counter if th enumber is placed in the array 
         //increment the counter 
         counter++; 
        } 

        //print the array 
        for (int j = 0; j < aList.Length; j++) 
        { 
         //exclude zeros 
         if (aList[j] == 0) 
         { 
          continue; 
         } 
         else 
         { 
          Console.WriteLine(aList[j]); 
         } 
        } 

        //reset the flag 
        isDuplicate = false; 
       } 
       else 
       { 
        Console.WriteLine("Numbers should be between 10 and 100"); 
       } 
      } 

#if DEBUG 
      Console.ReadKey(); 
#endif 

     } 
    } 
} 
+0

你刚才救了我从写这个答案的工作:D还我会用'counter',而不是'aList.Length'为两个循环。在这种情况下,*排除零* if语句将不是必要的......如果这段代码是由C#教师编写的,我会感到非常震惊! –

+0

我有一个链接到视频:)。这里也是我对一些电子邮件的回应摘录,当时我质疑了一些材料并解释了我所做的一些改变。 “这听起来像你只是无意中修改了陈述,因为我们想问”这个数字在10到100之间吗?“,这绝不会是一个AND语句,我们知道这将是一个双重条件或 如果我们考虑这个问题,由于我们不希望数字低于10,因此声明必须> = 10。同样,我们需要设置<= 100的上限,因为我们希望数字不超过100。 “ – Mike

+0

可能是获得退款的时间。该教师不理解基本的布尔逻辑,这对于课程来说不是一个好兆头。祝你好运。 –

相关问题