2016-10-07 11 views
0

私は今学期C#クラスを受験しています。私は配列でいくつかのことをする必要がある割り当てを持っています:配列に数値を追加する、配列に入力された数値を見る、配列内の数値を見つける、配列内の数値をソートする、最後にアプリケーションを終了します。配列に番号を追加する#検証番号のみ

これまでのところ、入力されたデータが数字だけであることを確認しながら、配列に数値を追加する際に少し問題がありました。私はそれを理解しようとしていると思うが、助けは常に感謝する。そして、私のfindData()メソッドは大丈夫ですか?

もう一度この質問を読んでいただきありがとうございます。

class Program 
{ 

    static char myChoice = Console.ReadKey().KeyChar; 
    static double[] myArray = new double[100]; 
    static void Main(string[] args) 
    { 



     while (true) 
     { 

      Console.WriteLine("Welcome to Lab 2"); 
      Console.WriteLine(); 
      Console.WriteLine("Main Menu"); 
      Console.WriteLine("1- Add new data"); 
      Console.WriteLine("2- See all data"); 
      Console.WriteLine("3- Find a number"); 
      Console.WriteLine("4- Sort the data"); 
      Console.WriteLine("5- Create statistics"); 
      Console.WriteLine("6- Exit Program"); 


      switch (myChoice) 
      { 
       case '1': 
        Console.WriteLine("1- Add new data"); 
        addData(); 
        break; 

       case '2': 
        Console.WriteLine("2- See all data"); 
        seeData(); 
        break; 

       case '3': 
        Console.WriteLine("3- Find a number"); 
        findData(); 
        break; 

       case '4': 
        Console.WriteLine("4- Sort the data"); 
        sortData(); 
        break; 

       case '5': 
        Console.WriteLine("5- Create statistics"); 
        createData(); 
        break; 

       case '6': 
        Console.WriteLine(); 
        exitProgram(); 
        break; 

      } 
     } 
    } 

    //This method will add numbers to the array 
    public static void addData() 
    { 
     bool isNumber = false; 
     double number; 
     double temp; 

     for (int i = 0; i < myArray.Length; i++) 
     { 
      Console.WriteLine("Enter a number you would like to add"); 

      myArray[i] = Convert.ToDouble(Console.ReadLine()); 
      temp = myArray[i]; 

      if (!Double.TryParse(temp, out number)) 
      { 
       Console.WriteLine("Invalid input. Please enter a valid number") 
      } 

      else 
      { 

      } 
     } 
    } 

    //This method will see the numbers entered in the array 
    public static void seeData() 
    { 
     foreach (var item in myArray) 
     { 
      Console.WriteLine(item.ToString()); 
     } 
    } 

    //This method will find a specific number within the array and check if it has already been entered 
    public static void findData() 
    { 

     Console.WriteLine("Find a number"); 
     string myChoice = Console.ReadLine(); 
     double number; 
     bool isNumber = Double.TryParse(myChoice, out number); 

     { 

     } 

    } 

    //This method will sort the array ascending to descending 
    public static void sortData() 
    { 
     Console.WriteLine("The array has been sorted in ascending order"); 
     Array.Sort(myArray); 
     Console.WriteLine("The array has been sorted in descending order"); 
     Array.Reverse(myArray); 
    } 

    //This method will create statistics based on the numbers in the array 
    public static void createData() 
    { 

     //Sum 
     double sum = myArray.Sum(); 
     Console.WriteLine("The total sum of the array is: " + sum); 

     //Average 
     double average = sum/myArray.Length; 
     Console.WriteLine("The average number of the array is: " + average); 

     //Maximum 
     double maximum = myArray.Max(); 
     Console.WriteLine("The maximum value in the array is: " + maximum); 

     //Minimum 
     double minimum = myArray.Min(); 
     Console.WriteLine("The minimum value in the array is: " + minimum); 

     //Mean 
     double mean = sum/myArray.Length; 
     Console.WriteLine("The mean average of the array is: " + mean); 
    } 

    //This method will exit the program 
    public static void exitProgram() 
    { 
     Environment.Exit(0); 
    } 
} 

}

+0

正確な問題は何ですか? – A3006

+0

[平均と算術平均はまったく同じです](http://stats.stackexchange.com/questions/14089/what-is-the-difference-between-mean-value-and-average)、許可されている場合Enumerable.Min、SumおよびMaxを使用するには、Enumerable.Averageを使用することもできます。また、ユーザが所望のアレイサイズを入力するように柔軟にすることもできる。 – Martheen

答えて

1

私findData()メソッドは大丈夫に見えるのでしょうか?

findData()メソッドは実際には何もしません。

はここであなたのaddData方法はあまり意味がありません

public static void addData() 
      { 
       for (int i = 0; i < myArray.Length; i++) 
       { 
        bool success = false; 
        while (!success) 
        { 
         Console.WriteLine("Enter a number you would like to add"); 
         string input = Console.ReadLine(); 
         double number; 
         if (Double.TryParse(input, out number)) 
         { 
          success = true; 
          myArray[i] = number; 
         } 

         else 
         { 
          Console.WriteLine("Invalid input. Please enter a valid number") 
         } 
        } 
       } 
      } 
0

一つのアプローチである:あなたが最初の配列にdouble値を挿入し、その後、あなたは確認してくださいその値がdouble型の場合(配列がdouble型であり、その型の値のみを含むことができるため、確かにそうです)。

また、Convert.ToDoubleは、ユーザー入力が有効でない場合は例外をスローすることがあります。しかし、私はDouble.TryParseメソッドの使用のポイントを得ることがわかります。このメソッドは、文字列(最初のパラメータ)が有効な数値であれば真を返します。

//This method will add numbers to the array 
public static void addData() 
{ 
    double number; 

    for (int i = 0; i < myArray.Length; i++) 
    { 
     Console.WriteLine("Enter a number you would like to add"); 
     // read user input 
     string input = Console.ReadLine(); 
     // condition is true if user input is a number 
     if (double.TryParse(input, out number)) 
      myArray[i] = number; 
     else 
      Console.WriteLine("Invalid input. Please enter a valid number"); 
    } 
} 

あなたはLINQを使用することができ、あなたの配列の番号を見つけるためにexaclyことない拡張メソッドが含まれます:だからあなたは、addData方法は次のようになります。配列は要素が含まれている場合、それはtrueを返す、そうでない場合はfalse:

//This method will find a specific number within the array and check if it has already been entered 
public static void findData() 
{ 
    double number; 
    Console.WriteLine("Find a number"); 
    string input = Console.ReadLine(); 
    // we use the same logic here as in the addData method to make sure the user input is a number 
    if (!double.TryParse(input, out number)) 
    { 
     bool found = myArray.Contains(number); 
     if (found) 
      Console.WriteLine("Array has number {0}", number); 
     else 
      Console.WriteLine("Array doesn't have number {0}", number); 
    } 
    else 
    { 
     Console.WriteLine("Invalid input. Please enter a valid number"); 
    } 
} 
0

これは、あなたのアドオンの問題解決する必要があります

public static void findData() 
{ 
    Console.WriteLine("Find a number"); 
    string myChoice = Console.ReadLine(); 
    double number = -1; 

    if(!Double.TryParse(myChoice, out number)) 
    { 
     Console.WriteLine("Invalid number"); 
    } 
    else if (Array.IndexOf<double>(myArray, number) == -1) 
    { 
     Console.WriteLine("Number does not exist"); 
    } 
    else 
    { 
     Console.WriteLine("Number does exist"); 
    } 
} 
+2

入力が無効な場合、forループは引き続き実行されます –

+0

@CodeJoyはい、その場合、配列要素にはdoubleのデフォルト値、つまり0.0dが含まれているため、プログラムの実行が損なわれません。また、質問はその状況におけるメソッドの動作を指定しないので、コードは完全に有効です。それ以外の場合には、ユーザーの入力が有効であるかどうかを示すブール値フラグを持つ内部do-whileループを使用することが理にかなっています。 – Alex

+0

@Alexなぜ入力が無効な場合は--iだけではないのですか? – Martheen

関連する問題