编程中控制台应用程序动态数组最大值最小值用arraylist如何编程
展开全部
以C#输入10个数存入数组,求最大值、最小值为例:
*/
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("这是一个输入10个数存入数组,求最大值、最小值和平均值的程序");
double[] c = new double[10];
for (int number = 0; number < 10; ++number)
{
Console.Write("请您输入第{0}个数:", number + 1);
// string s = Console.ReadLine();
c[number] = double.Parse(Console.ReadLine());
}
//利用foreach语句循环输出数组中每一个元素
Console.Write("您输入10个数为:");
foreach (double var in c)
{
Console.Write(var); //依次读取字符串中的元素
Console.Write(" ");
}
Console.WriteLine();
double min1 = min(c);
Console.WriteLine("您输入10个数中最小值为:{0}",min1);
double max1 = max(c);
Console.WriteLine("您输入10个数中最大值为:{0}",max1);
double average1 = average(c);
Console.WriteLine("您输入10个数中平均值为:{0}",average1);
Console.ReadKey();
}
static double max(double[] c)
{
double max = c[0];
for (int i = 1; i < 10; ++i)
{
if (max < c[i])
max = c[i];
}
return max;
}
static double min(double[] c1)
{
double min = c1[0];
for (int i = 1; i < 10; ++i)
{
if (min > c1[i])
min = c1[i];
}
return min;
}
static double average(double[] c1)
{
double average;
double all=0;
/* foreach (char var in c1)
{
all = all + var;
}*/
for (int i = 0; i < 10; ++i)
{
all = all + c1[i];
}
average = all / c1.Length;
return average;
}
}
}
运行结果:
附件:
下面这个程序是错误的,他是计算的数字对应的ASCII的最大值。
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("这是一个输入10个数存入数组,求最大值、最小值和平均值的程序");
Console.Write("请您输入10个数(数与数之间用空格断开):");
string s = Console.ReadLine();
char[] c = s.ToCharArray();
int i = c.Length;
int j = 0;
int[] c1 = new int[c.Length];
//利用foreach语句循环输出数组中每一个元素
foreach (char var in c)
{
//c1[j] = Convert.ToInt32(var);//转化成了字符的ASCII码
c1[j] = var;
++j; //字符串读取
}
Console.Write("您输入10个数为:");
foreach (char var in c1)
{
Console.Write(var); //依次读取字符串中的元素
}
Console.WriteLine();
int min1 = min(c1);
Console.WriteLine("您输入10个数中最小值为:{0}",min1);
int max1 = max(c1);
Console.WriteLine("您输入10个数中最大值为:{0}",max1);
Console.ReadKey();
}
static int max(int[] c1)
{
int n = c1.Length;
int max = c1[0];
for (int i = 2; i < n; i = i+2)
{
if (max < c1[i])
max = c1[i];
}
return max;
}
static int min(int[] c1)
{
int n = c1.Length;
int min = c1[0];
for (int i = 2; i < n; i = i + 2)
{
if (min > c1[i])
min = c1[i];
}
return min;
}
static double average(int[] c1)
{
double average;
int all=0;
/* foreach (char var in c1)
{
all = all + var;
}*/
for (int i = 0; i < c1.Length; i = i + 2)
{
all = all + c1[i];
}
average = all / c1.Length;
return average;
}
}
*/
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("这是一个输入10个数存入数组,求最大值、最小值和平均值的程序");
double[] c = new double[10];
for (int number = 0; number < 10; ++number)
{
Console.Write("请您输入第{0}个数:", number + 1);
// string s = Console.ReadLine();
c[number] = double.Parse(Console.ReadLine());
}
//利用foreach语句循环输出数组中每一个元素
Console.Write("您输入10个数为:");
foreach (double var in c)
{
Console.Write(var); //依次读取字符串中的元素
Console.Write(" ");
}
Console.WriteLine();
double min1 = min(c);
Console.WriteLine("您输入10个数中最小值为:{0}",min1);
double max1 = max(c);
Console.WriteLine("您输入10个数中最大值为:{0}",max1);
double average1 = average(c);
Console.WriteLine("您输入10个数中平均值为:{0}",average1);
Console.ReadKey();
}
static double max(double[] c)
{
double max = c[0];
for (int i = 1; i < 10; ++i)
{
if (max < c[i])
max = c[i];
}
return max;
}
static double min(double[] c1)
{
double min = c1[0];
for (int i = 1; i < 10; ++i)
{
if (min > c1[i])
min = c1[i];
}
return min;
}
static double average(double[] c1)
{
double average;
double all=0;
/* foreach (char var in c1)
{
all = all + var;
}*/
for (int i = 0; i < 10; ++i)
{
all = all + c1[i];
}
average = all / c1.Length;
return average;
}
}
}
运行结果:
附件:
下面这个程序是错误的,他是计算的数字对应的ASCII的最大值。
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("这是一个输入10个数存入数组,求最大值、最小值和平均值的程序");
Console.Write("请您输入10个数(数与数之间用空格断开):");
string s = Console.ReadLine();
char[] c = s.ToCharArray();
int i = c.Length;
int j = 0;
int[] c1 = new int[c.Length];
//利用foreach语句循环输出数组中每一个元素
foreach (char var in c)
{
//c1[j] = Convert.ToInt32(var);//转化成了字符的ASCII码
c1[j] = var;
++j; //字符串读取
}
Console.Write("您输入10个数为:");
foreach (char var in c1)
{
Console.Write(var); //依次读取字符串中的元素
}
Console.WriteLine();
int min1 = min(c1);
Console.WriteLine("您输入10个数中最小值为:{0}",min1);
int max1 = max(c1);
Console.WriteLine("您输入10个数中最大值为:{0}",max1);
Console.ReadKey();
}
static int max(int[] c1)
{
int n = c1.Length;
int max = c1[0];
for (int i = 2; i < n; i = i+2)
{
if (max < c1[i])
max = c1[i];
}
return max;
}
static int min(int[] c1)
{
int n = c1.Length;
int min = c1[0];
for (int i = 2; i < n; i = i + 2)
{
if (min > c1[i])
min = c1[i];
}
return min;
}
static double average(int[] c1)
{
double average;
int all=0;
/* foreach (char var in c1)
{
all = all + var;
}*/
for (int i = 0; i < c1.Length; i = i + 2)
{
all = all + c1[i];
}
average = all / c1.Length;
return average;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询