C# 数组中的最大数和最小数
这非常有效……但是当我使用C# 数组中的最大数和最小数,c#,C#,这非常有效……但是当我使用foreach而不是for时,这不起作用。我不明白for和foreach是一样的 namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int[] array = new int[10]; Console.WriteLine("enter the array elem
foreach
而不是for
时,这不起作用。我不明白for
和foreach
是一样的
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[10];
Console.WriteLine("enter the array elements to b sorted");
for(int i=0;i<10;i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
int smallest = array[0];
for(int i=0;i<10;i++)
{
if(array[i]<smallest)
{
smallest=array[i];
}
}
int largest = array[9];
for(int i=0;i<10;i++)
{
if (array[i] > largest)
{
largest = array[i];
}
}
Console.WriteLine("the smallest no is {0}", smallest);
Console.WriteLine("the largest no is {0}", largest);
Console.Read();
}
}
}
命名空间控制台应用程序2
{
班级计划
{
静态void Main(字符串[]参数)
{
int[]数组=新的int[10];
WriteLine(“输入要排序的数组元素”);
对于(inti=0;i为什么不使用这个
int[] array = { 12, 56, 89, 65, 61, 36, 45, 23 };
int max = array.Max();
int min = array.Min();
使用foreach时(通常)无法修改正在迭代的集合
虽然for和foreach从开发人员的角度来看似乎很相似,但从实现的角度来看却大不相同
Foreach
使用迭代器
访问单个对象,而for
不知道(或关心)底层对象序列。如果出于某种原因需要使用Foreach并且不想在函数中使用bult,下面是一个代码片段:
int minint = array[0];
int maxint = array[0];
foreach (int value in array) {
if (value < minint) minint = value;
if (value > maxint) maxint = value;
}
int minint=array[0];
int maxint=数组[0];
foreach(数组中的int值){
如果(值<最小值)最小值=值;
如果(值>最大值)最大值=值;
}
使用系统;
命名空间最大
{
一流的
{
公共静态void Main(字符串[]args)
{
//获取元素的数量
Console.WriteLine(“输入元素数”);
int i;
i=Convert.ToInt32(Console.ReadLine());
int[]abc=新的int[i];
//接受元素
对于(int size=-1;sizestatic void printmallest最大值(int[]arr)
{
如果(arr.Length>0)
{
int small=arr[0];
int-large=arr[0];
对于(int i=0;iarr[i])
{
int tmp=小;
小=arr[i];
arr[i]=小;
}
}
WriteLine(“最小的是{0}”,小);
WriteLine(“最大的是{0}”,大);
}
}
这样,您可以在单个循环中拥有最小和最大的数字。Int[]number={1,2,3,4,5,6,7,8,9,10};
Int[] number ={1,2,3,4,5,6,7,8,9,10};
Int? Result = null;
foreach(Int i in number)
{
If(!Result.HasValue || i< Result)
{
Result =i;
}
}
Console.WriteLine(Result);
}
Int?结果=null;
foreach(整数i)
{
如果(!Result.HasValue | | i
public int MinimumValue{get;private set;}
public int MaxmimumValue{get;private set;}
public void num()
{
int[]数组={12,56,89,65,61,36,45,23};
MaxmimumValue=数组[0];
最小值=数组[0];
foreach(数组中的int num)
{
如果(num>MaxmimumValue)MaxmimumValue=num;
如果(num
以下是完整的程序`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
namespace oops3
{
public class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Enter the size of the array");
int x = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[x];
Console.WriteLine("Enter the elements of the array");
for(int i=0;i<x;i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
int smallest = arr[0];
int Largest = arr[0];
for(int i=0;i<x;i++)
{
if(smallest>arr[i])
{
smallest = arr[i];
}
}
for (int i = 0; i < x; i++)
{
if (Largest< arr[i])
{
Largest = arr[i];
}
}
Console.WriteLine("The greater No in the array:" + Largest);
Console.WriteLine("The smallest No in the array:" + smallest);
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统线程;
使用系统诊断;
命名空间oops3
{
公开课演示
{
静态void Main(字符串[]参数)
{
WriteLine(“输入数组的大小”);
intx=Convert.ToInt32(Console.ReadLine());
int[]arr=新的int[x];
WriteLine(“输入数组的元素”);
对于(int i=0;i使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间数组\u Small\u和\u lagest{
班级计划{
静态void Main(字符串[]参数){
int[]数组=新的int[10];
WriteLine(“输入要排序的数组元素”);
对于(int i=0;i<10;i++){
数组[i]=Convert.ToInt32(Console.ReadLine());
}
int最小=数组[0];
foreach(数组中的int i){
如果(i<最小值){
最小=i;
}
}
int最大=数组[9];
foreach(数组中的int i){
如果(i>最大值){
最大=i;
}
}
WriteLine(“最小的no是{0}”,最小的);
WriteLine(“最大的no是{0}”,最大);
Console.Read();
}
}
}
这是一段很长的时间。可能是这样的:
public int smallestValue(int[] values)
{
int smallest = int.MaxValue;
for (int i = 0; i < values.Length; i++)
{
smallest = (values[i] < smallest ? values[i] : smallest);
}
return smallest;
}
public static int largestvalue(int[] values)
{
int largest = int.MinValue;
for (int i = 0; i < values.Length; i++)
{
largest = (values[i] > largest ? values[i] : largest);
}
return largest;
}
public int smallestValue(int[]值)
{
int最小值=int.MaxValue;
for(int i=0;i最大?值[i]:最大);
}
回报最大;
}
通用扩展方法(在一次迭代中获取Min
和Max
):
公共静态类MyExtension
{
公共静态(T最小,T最大)最小(此IEnumerable源),其中T:IComparable
{
if(source==null)
{
抛出新ArgumentNullException(nameof(source));
}
T min=source.FirstOrDefault();
T max=source.FirstOrDefault();
foreach(源中的T项)
{
如果(项目比较(最小值)=-1)
{
最小值=项目;
}
如果(项目比较(最大)=1)
{
最大值=项目;
}
}
返回(最小值)
public int MinimumValue { get; private set; }
public int MaxmimumValue { get; private set; }
public void num()
{
int[] array = { 12, 56, 89, 65, 61, 36, 45, 23 };
MaxmimumValue = array[0];
MinimumValue = array[0];
foreach (int num in array)
{
if (num > MaxmimumValue) MaxmimumValue = num;
if (num < MinimumValue) MinimumValue = num;
}
Console.WriteLine(MinimumValue);
Console.WriteLine(MaxmimumValue);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
namespace oops3
{
public class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Enter the size of the array");
int x = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[x];
Console.WriteLine("Enter the elements of the array");
for(int i=0;i<x;i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
int smallest = arr[0];
int Largest = arr[0];
for(int i=0;i<x;i++)
{
if(smallest>arr[i])
{
smallest = arr[i];
}
}
for (int i = 0; i < x; i++)
{
if (Largest< arr[i])
{
Largest = arr[i];
}
}
Console.WriteLine("The greater No in the array:" + Largest);
Console.WriteLine("The smallest No in the array:" + smallest);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array_Small_and_lagest {
class Program {
static void Main(string[] args) {
int[] array = new int[10];
Console.WriteLine("enter the array elements to b sorted");
for (int i = 0; i < 10; i++) {
array[i] = Convert.ToInt32(Console.ReadLine());
}
int smallest = array[0];
foreach (int i in array) {
if (i < smallest) {
smallest = i;
}
}
int largest = array[9];
foreach (int i in array) {
if (i > largest) {
largest = i;
}
}
Console.WriteLine("the smallest no is {0}", smallest);
Console.WriteLine("the largest no is {0}", largest);
Console.Read();
}
}
}
public int smallestValue(int[] values)
{
int smallest = int.MaxValue;
for (int i = 0; i < values.Length; i++)
{
smallest = (values[i] < smallest ? values[i] : smallest);
}
return smallest;
}
public static int largestvalue(int[] values)
{
int largest = int.MinValue;
for (int i = 0; i < values.Length; i++)
{
largest = (values[i] > largest ? values[i] : largest);
}
return largest;
}
public static class MyExtension
{
public static (T Min, T Max) MinMax<T>(this IEnumerable<T> source) where T : IComparable<T>
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
T min = source.FirstOrDefault();
T max = source.FirstOrDefault();
foreach (T item in source)
{
if (item.CompareTo(min) == -1)
{
min = item;
}
if (item.CompareTo(max) == 1)
{
max = item;
}
}
return (Min: min, Max: max);
}
}