C#语言中怎样实现输入4个整数,求出其最大值和最小值

C#语言中怎样实现输入4个整数,求出其最大值和最小值,请写出详细的程序代码,注:是C#语言而不是C语言,C++语言,或者是其他的语言 。

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

namespace Conslx5 //这取决于你建立控制台程序时所取的名字;
{
class Program
{
static void Main(string[] args)
{
string [] a=new string [3]; //定义一个长度为4的一维数组,字符串型
string input;//定义用于接收输入的字数串
Console.WriteLine("请输入用逗号分隔的四个整数:");
input = Console.ReadLine();
a = input.Split(','); //将输入分开放入数组中

long max = Convert.ToInt64(a[0]), min = Convert.ToInt64(a[0]);

//定义了最大值,最小值,这里解释一下,long和int64指的是一个长度
//max ,min 必须在foreach循环外定义和初始化!!!

foreach (string aa in a)
{
if( Convert.ToInt64(aa) >max){max=Convert.ToInt64(aa);}
if (Convert.ToInt64(aa) < min) { min = Convert.ToInt64(aa); }
//这两行是比较每个值,把最大值放在max,最小值放在min里
}
Console.WriteLine("最大值是{0},最小值是{1}", max, min);
Console.ReadKey();
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-08-10
using System;
using System.Collections.Generic;
using System.Text;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入四个整数,用空格分开:");
            string str = Console.ReadLine();
            string [] nums = input.Split(' ');
            long max = Convert.ToInt64(a[0]), min = max, tem;
            foreach (string s in nums)
            {
                tem = Convert.ToInt64(s);
                if (tem > max) max = tem;
              else if (tem < min) min = tem;
            }
            Console.WriteLine("最大值是{0},最小值是{1}", max, min);
            Console.ReadKey();
        }
    }
}

第2个回答  2013-09-25
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class InputBox : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;

private InputBox()
{
InitializeComponent();
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// InputBox
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 53);
this.ControlBox = false;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "InputBox";
this.Text = "InputBox";
this.ResumeLayout(false);

}

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
this.Close();
}

public static string ShowInputBox()
{
InputBox box = new InputBox();
box.ShowDialog();
return box.textBox1.Text;
}
}

namespace 整数排大小
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int[] a;
a=new int[4];
int i,min,max;
for (i = 0; i < 4; i++)
{
a[i] =Convert .ToInt16 ( InputBox.ShowInputBox());

}
min = max = a[0];
for (i = 0; i < 4; i++)
{
if (a[i] < min) min = a[i];
if (a[i] > max) max = a[i];
}
MessageBox.Show("最小值为" + min + ",最大值为" + max);
}
}
}
第3个回答  2013-09-25
在Visual Studio 2008中,建立一个使用.NET Framework 3.5的控制台程序

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num1 = Int32.Parse(Console.ReadLine());
int num2 = Int32.Parse(Console.ReadLine());
int num3 = Int32.Parse(Console.ReadLine());
int num4 = Int32.Parse(Console.ReadLine());

int[] array = new int[4] { num1, num2, num3, num4 };
int max = array.Max();
int min = array.Min();

Console.WriteLine("Max: {0}, Min: {1}", max, min);
}
}
}本回答被网友采纳
相似回答