我是新的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();
}
}
}
歡迎來到SO!你能告訴我們你試過了什麼嗎? – AlG 2012-01-27 20:51:24
首先嚐試製定一個問題的陳述,然後解決它在[僞](http://en.wikipedia.org/wiki/Pseudocode)。以類似的方式開始:「用戶輸入五個數字,其中,刪除最高和最低值,並返回剩餘數字的平均值」。 :-) – Rytmis 2012-01-27 20:55:58
我得到了其中一個與我在這一刻的工作,在這裏它是 – user1174357 2012-01-27 20:56:46