我试图找到三个用户输入数字的中位数。请参阅下面的代码,感谢任何帮助和新手解释为什么。根据我的理解,他们应该被分类到一个列表,然后找到中间的数字。我想以一种强有力的方式输出以下内容,以防我想要询问用户4个数字。
我想输出:输入的数字:x,y,z 最小值:a 中位数价值:b
using System;
public class Assignment
{
public static void Main()
{
int num1;
int num2;
int num3;
Console.WriteLine("Enter 3 numbers");
num1 = Int32.Parse(Console.ReadLine());
num2 = Int32.Parse(Console.ReadLine());
num3 = Int32.Parse(Console.ReadLine());
Console.WriteLine("Numbers entered: {0} {1} {2}", num1, num2, num3);
if(num1<num2)
if(num2<num3)
{
Console.WriteLine("Minimum Value:"+num1);
}
else
{
Console.WriteLine("Minimum Value:"+num3);
}
else
if(num2<num3)
{
Console.WriteLine( "Minimum Value:"+num2);
}
else
{
Console.WriteLine("Minimum Value:"+num3 );
}
}
}
答案 0 :(得分:2)
闻起来像家庭作业
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int num3 = Convert.ToInt32(Console.ReadLine());
List<int> list1 = new List<int>();
list1.Add(num1);
list1.Add(num2);
list1.Add(num3);
Console.WriteLine($"Min:{list.Min()}");
list1.Sort();
int c = list1.Count()%2==0?list1.Count()+1:list1.Count();
int Med = list1[c/2];
//Console.WriteLine($"Max:{list1.Max()");
我正在打电话,所以格式化我的代码很难.. 您可以跳过创建三个整数并使用
list1.Add(Int.Parse(Console.ReadLine()));
如果你愿意或者'n'数字
bool b = true;
int num=0;
List<int>() l = new List<int>();
Do{
Console.Write("Enter number: ");
b =
int.TryParse(Console.ReadLine(), out num);
l.Add(num);
}While(b);
//insert min, max and median of (List) l here
答案 1 :(得分:0)
你的代码不会编译它现在的编写方式,但我可以看到你正在尝试做什么。你可以改变它以便编译,但它不是一种特别有效的解决方法。
现在,我认为您正在尝试编写代码来检查潜在数字顺序的每个场景。您可以相对容易地为3个数字执行此操作,但如果您想扩展代码以检查10或100个数字的列表,该怎么办?
我认为您应该将用户输入添加到列表中,对列表进行排序,然后只返回列表的中间值。代码更少,更可靠。
public class FindMedian
{
public List<int> numbersList = new List<int>(); // list to store user input
// constructor
public FindMedian(int n)
{
Console.WriteLine("Please Enter " + n + " numbers. \n\n");
for (int i = 1; i < n + 1; i++)
{
Console.WriteLine("Number " + i + " : ");
numbersList.Add(GetUserInput()); // call's GetUserInput
}
PrintData();
}
// returns int from Console. Will continue to run until a valid int is entered
private int GetUserInput()
{
int temp = 0;
bool numberValid = false;
while (!numberValid)
{
try
{
temp = int.Parse(Console.ReadLine());
numberValid = true;
}
catch
{
Console.WriteLine("Invalid entry. Please try again.\n");
numberValid = false;
}
}
return temp;
}
// prints data after user enteres the correct number of ints
private void PrintData()
{
// If list contains no data
if (numbersList.Count < 3)
{
Console.WriteLine("No User Data Entered");
return;
}
numbersList.Sort(); // Sorts list from smallest to largest
int minimum = numbersList[0];
int median;
// if list count is even and median is average of two middle numbers
if (numbersList.Count % 2 == 0)
{
median = (numbersList[(numbersList.Count / 2) - 2] + numbersList[(numbersList.Count / 2) - 1] / 2);
}
// if list count is odd and median is just middle number
else
{
median = numbersList[(numbersList.Count / 2)];
}
Console.WriteLine("Minimum Number : " + minimum);
Console.WriteLine("Median Number : " + median);
}
}