2012-01-27 39 views
0

我是新的C#,很好,編碼一般。 我已經做得相當好由我自己到今天爲止,在此介紹當然我走,但我遇到了一個道路隆起。使用「如果」語句刪除最小/最大用戶輸入數據

我想弄清楚如何編碼一個if語句,將在循環內運行,以分析5個不同的整數,因爲它們被輸入並將max int和min int分開,以便我可以使用其餘三個ints做一個計算。 確切地說,驗證用戶輸入並刪除最小/最大用戶輸入來平均餘下的三個。

PS,我試過一個數組,但由於某種原因,它不能正常工作很好。儘管我現在在工作,但我沒有代碼。我在講座中被告知應該使用if語句,但是數組也是可能的。

感謝您的時間和任何可能的答案。

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string userIsFinished = ""; 
      string name, city, value; 
      double rating, avg = 0; 
      double[] array1 = new double[5]; 
      double max = 0; 
      double min = double.MaxValue; 
      double score, totalScore = 0; 


      //get basic information 
      do 
      { 
       Console.WriteLine("Please enter divers name."); 
       name = Console.ReadLine(); 
       Console.WriteLine("Please enter the divers city."); 
       city = Console.ReadLine(); 



       //get and validate user input for 1 dive rating 
       Console.WriteLine("Please enter a dive rating between 1.00 and 1.67."); 
       rating = Double.Parse(Console.ReadLine()); 
       while (rating < 1 || rating > 1.69) 
       { 
        Console.WriteLine("Oops, you entered an invalid number. Please, enter a dive rating between 1.00 and 1.67."); 
        rating = Double.Parse(Console.ReadLine()); 
       } 
       Console.ReadLine(); 

       // get and validate user input for 5 judge scores 
       for (int s = 1; s <= 5; s++) 
       { 
        Console.WriteLine("Please enter the score for judge {0}.", s); 
        value = Console.ReadLine(); 
        score = Convert.ToDouble(value); 
        while (score < 0 || score > 10) 
        { 
         Console.WriteLine("Invalid entry, please enter a number in between 0 - 10."); 
         score = Convert.ToDouble(Console.ReadLine()); 
        } 
        array1[s] = Convert.ToDouble(score);  //----this line keeps throwing an exception 
       } 
       Console.ReadLine(); 

       //calculate totalScore by dropping min/max scores and averaging them times dive rating 
       foreach (int i in array1) 
       { 
        if (i > max) 
         max = i; 
        if (i < min) 
         min = i; 
        avg += i; 
       } 
       totalScore = avg * rating; 

       //Print gathered and calculated information 
       Console.WriteLine("Divers name: {0}", name); 
       Console.WriteLine("Divers city: {0}", city); 
       Console.WriteLine("Dive degree of difficulty: {0}", rating); 
       Console.WriteLine("Total dive score is: {0}", totalScore); 

       // Ask if user wants to process another diver and continue or exit program 
       Console.WriteLine("Would you like to enter another divers information? [Y]es [N]o: "); 
       userIsFinished = Console.ReadLine(); 
      } 
      while 
      (userIsFinished.ToLower() != "n"); 
      Console.ReadLine(); 
     } 
    } 
} 
+4

歡迎來到SO!你能告訴我們你試過了什麼嗎? – AlG 2012-01-27 20:51:24

+2

首先嚐試製定一個問題的陳述,然後解決它在[僞](http://en.wikipedia.org/wiki/Pseudocode)。以類似的方式開始:「用戶輸入五個數字,其中,刪除最高和最低值,並返回剩餘數字的平均值」。 :-) – Rytmis 2012-01-27 20:55:58

+0

我得到了其中一個與我在這一刻的工作,在這裏它是 – user1174357 2012-01-27 20:56:46

回答

1

不知道你的問題...元素你想知道的,關於三人的最大值和最小值約5的值,而avarage ...

int[] n = { 4, 7, 29, 3, 87 }; 
int max = 0; 
int min = int.MaxValue; 
double avg = 0; 

foreach (int i in n) 
{ 
    if (i > max) 
     max = i; 

    if (i < min) 
     min = i; 

    avg += i; 
} 

avg = avg/n.Count - 2; 
+0

我得到了完整的代碼,如果你可以,給我看看我? – user1174357 2012-01-28 00:44:13

1

試試這個代碼:

int[] a = new int[5]; 
int minpos; 
int maxpos; 
int min = Int32.MaxValue; 
int max = a[0]; 
int temp = 0; 
for (int i = 0; i < 5; i++) 
{ 
    Console.WriteLine(" Enter number " + (i + 1)); 
    Int32.TryParse(Console.ReadLine(), out temp); 
    a[i] = temp; 

    //Decision Making Logic 

    if (min > temp) 
    { 
     min = temp; 
     minpos = i; 
    } 
    if (max < temp) 
    { 
     max = temp; 
     maxpos = i; 
    } 
} 

//在這個循環結束時,你會看到minpos包含最小元素的索引 //和maxpos包含最大元素的索引值剩餘的indeces包含既不是最大或最小的是//集合中

+0

iam真的對不起,如果你發現代碼難以辨認,因爲iam還沒有與代碼格式同步 – AnandNagarajan111 2012-01-27 21:13:42

+0

我可以讀它的隊友,謝謝。在這裏有很好的信息。 – user1174357 2012-01-28 00:37:51

2

,或者你可以去列表路線和

List<int> apples = new List<int>(); 
apples.Add(31); 
apples.Add(34); 
apples.Add(100); 
apples.Add(57); 
apples.Add(1); 
int min = apples.Min(); 
int max = apples.Max(); 
apples.Remove(min); 
apples.Remove(max); 
decimal average = (decimal)(apples.Sum())/apples.Count; 
+1

畢竟'Max()','Min()'爲什麼不使用'apples.Average()' – 2012-01-27 21:26:14

+0

謝謝了,很遺憾,我們還沒有那麼深入課堂,但這看起來很有趣,我在我們得到這些之前,我們將開始研究它。 – user1174357 2012-01-27 23:00:03

+0

@ L.嗯,是啊,好問題:) – peroija 2012-01-28 03:19:09

0

謝謝你們,看來我需要一個良好的夜間睡眠。非常感謝所有這些有用的答案,因爲我確信我將盡快研究這些方法,能夠在他們身上取得領先優勢將是一件好事。這裏是我的代碼,

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string userIsFinished = ""; 
      string name, city; 
      double rating, avg = 0; 
      double max = 0; 
      double min = 10; 
      double score, value = 0, totalScore = 0, finalScore = 0; 


      //get basic information 
      do 
      { 
       Console.WriteLine("\n"); 
       Console.WriteLine("Please enter divers name."); 
       name = Console.ReadLine(); 
       Console.WriteLine("Please enter the divers city."); 
       city = Console.ReadLine(); 



       //get and validate user input for 1 dive rating 
       Console.WriteLine("Please enter a dive rating between 1.00 and 1.67."); 
       rating = Double.Parse(Console.ReadLine()); 
       while (rating < 1 || rating > 1.69) 
       { 
        Console.WriteLine("Oops, you entered an invalid number. Please, enter a dive rating between 1.00 and 1.67."); 
        rating = Double.Parse(Console.ReadLine()); 
       } 
       Console.ReadLine(); 

       // get and validate user input for 5 judge scores 
       for (int s = 1; s <= 5; s++) 
       { 
        Console.WriteLine("Please enter the score for judge {0}.", s); 
        score = Convert.ToDouble(Console.ReadLine()); 

        while (score < 0 || score > 10) 
        { 
         Console.WriteLine("Invalid entry, please enter a number in between 0 - 10."); 
         score = Convert.ToDouble(Console.ReadLine()); 
        } 
        if (score > max) 
         max = score; 
        if (score < min) 
         min = score; 

        totalScore = score + totalScore; 
       } 
       Console.ReadLine(); 
       \\Calculate values 
       value = totalScore - max - min; 
       avg = value/3; 
       finalScore = avg * rating; 

       //Print gathered and calculated information 
       Console.WriteLine("Divers name: {0}", name); 
       Console.WriteLine("Divers city: {0}", city); 
       Console.WriteLine("Dive degree of difficulty: {0}", rating); 
       Console.WriteLine("Total dive score is: {0}", finalScore); 
       Console.WriteLine("\n"); 

       // Ask if user wants to process another diver and continue or exit program 
       Console.WriteLine("Would you like to enter another divers information? [Y]es [N]o: "); 
       userIsFinished = Console.ReadLine(); 
      } 
      while 
      (userIsFinished.ToLower() != "n"); 
      Console.ReadLine(); 
      Console.WriteLine("\n"); 
     } 
    } 
} 
相關問題