我需要用户输入三个数字,然后我需要让程序写出最大到最小的数字。
例如: USER输入16,3和45 输出将是:最大数字是:45,中间数字是:16,最小数字是:3;
此刻我将数字存储在不同的变量中并使用if,否则if语句获得最大和最小,但我不知道如何使用else获取中间数字。
if (number1 <= number2
&& number1 >= number3 & number1 <= number3
&& number1 >= number2)
{
middle = number1;
}
else if (number2 <= number1
&& number2 >= number3 & number2 <= number3
&& number2 >= number1)
{
middle = number2;
}
else if (number3 <= number1
&& number3 >= number2 & number3 <= number2
&& number3 >= number1)
{
middle = number3;
}
甚至可能吗?
我是否需要让用户将值输入到Array中,然后我需要对其进行排序,然后写出已排序的数组?
编辑:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practise
{
class Practise
{
static void Main(string[] args)
{
int[] newArray = new int[3];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = Convert.ToInt32(Console.ReadLine());
}
Array.Sort(newArray);
int lowestNumber = newArray[0];
int middle = newArray[1];
int highest = newArray[2];
Console.WriteLine("How can i only type out variables?" + highest + How can i only type out variables?" + middle + "How can i only type out variables?" + lowestNumber);
Console.ReadLine();
}
}
}
Array排序工作,使用了Selman22的解决方案! 我需要在Console.WriteLine()中输入什么内容;只写出没有添加值的变量。
> example: highest = 15 middle = 10 lowestNumber = 5
> Console.WriteLine(highest + middle + lowestNumber); would do 15 + 10
> + 5 = 30. If i put "text" in between they obviously does not add up but what do I type to only get values out?
我非常感谢所有答案,这个网站真的很有帮助!
编辑:
Console.WriteLine(" " + highest + " " + middle + " " + lowestNumber);
有没有其他方法写出变量的值而不是在两者之间添加“”? 如果我做
Console.Writeline(highest, middle ,lowestNumber);
如果我说它说错误就像无法将最高转换成字符串等。
答案 0 :(得分:4)
我是否需要让用户将值输入到Array中,然后我需要对其进行排序,然后写出已排序的数组?
是的,如果你这样做会简单得多。只需将用户输入存储到数组中然后对其进行排序
var numbers = new int[] { number1, number2, number3 };
Array.Sort(numbers);
int lowestNumber = numbers[0];
int middle = numbers[1];
int highest = numbers[2];
如果您希望用户输入三个以上,则可以轻松扩展。
答案 1 :(得分:2)
尝试使用数组或列表存储您的所有数字。以下是使用Linq命令列表的方法示例。
var myNumbers = new List<int>();
myNumbers.Add(45);
myNumbers.Add(14);
myNumbers.Add(30);
//etc
var sortedList = myNumbers.OrderBy(x => x).ToList();
//sortedList now contains a list of numbers in order from large to small.
var middleNumber = sortedList[1]; //get the second entry.
答案 2 :(得分:0)
为了与众不同,我将提供一个不使用数组的答案,以便与使用数组产生一些对比。也回答问题&#34;是否可能&#34; (是的,是的)。
int minimum, middle, maximum; // Use whatever numerical type is appropriate here.
if( number1 < number2 )
{
if( number2 < number3 )
{
minimum = number1;
middle = number2;
maximum = number3;
}
else if( number1 < number3 )
{
minimum = number1;
middle = number3;
maximum = number2;
}
else
{
minimum = number3;
middle = number1;
maximum = number2;
}
}
else
{
if( number1 < number3 )
{
minimum = number2;
middle = number1;
maximum = number3;
}
else if( number2 < number3 )
{
minimum = number2;
middle = number3;
maximum = number1;
}
else
{
minimum = number3;
middle = number2;
maximum = number1;
}
}
答案 3 :(得分:0)
这是另一种使用SortedSet<int>
的方法,它不包含重复项。但是,无论如何都无需确定最低,中等和最高数字:
var numbers = new SortedSet<int>();
numbers.Add(16); numbers.Add(3); numbers.Add(45);
Console.WriteLine(" {0} {1} {2}",
numbers.Max,
numbers.ElementAt(1),// use numbers.Count/2 instead of 1 to determine the middle progr.
numbers.Min);