2011-02-05 77 views
6

Công trình này hoàn hảo ... nhưng khi tôi sử dụng foreach thay vì for thì điều này không có tác dụng. Tôi không thể hiểu được forforeach giống nhau.Số lớn nhất và nhỏ nhất trong một mảng

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(); 


     } 
    } 
} 
+1

Hiển thị mã cho bạn mà bạn đã thử không hoạt động. có thể bạn đang làm sai điều gì đó –

+0

Điều này không hiệu quả vì cách bạn viết câu hỏi của mình. : P (Ok, nó đã được chỉnh sửa, bình luận của tôi không còn có ý nghĩa) –

+0

Tôi tò mò tại sao bạn nói 'int lớn nhất = mảng [9];'. Tại sao không lấy phần tử đầu tiên theo mặc định? –

Trả lời

25

Tại sao bạn không sử dụng?

int[] array = { 12, 56, 89, 65, 61, 36, 45, 23 }; 
int max = array.Max(); 
int min = array.Min(); 
+4

Mùi giống như bài tập về nhà. Giáo viên của anh ta có lẽ đã không đến đó. –

+0

Nếu đó là bài tập về nhà, anh ấy có khả năng đang cố gắng thực hiện phương pháp này thay vì sử dụng một phương pháp tích hợp sẵn (và có thể tìm ra các bất biến vòng lặp). –

+0

+1 nếu bạn cho tôi biết nơi tôi có thể tìm thấy Max(): p, nó không có vẻ là trong System.Array, tôi đang sử dụng một [] –

2

Bạn (thường) không thể sửa đổi bộ sưu tập bạn đang lặp lại khi sử dụng foreach.

Mặc dù cho và foreach có vẻ tương tự với góc nhìn của nhà phát triển nhưng chúng hoàn toàn khác với quan điểm triển khai.

Foreach sử dụng một số Iterator để truy cập các đối tượng riêng lẻ trong khi for không biết (hoặc quan tâm) về chuỗi đối tượng cơ bản.

8

Nếu bạn cần phải sử dụng foreach (đối với một số lý do) và không muốn sử dụng chức năng Bult-in, đây là một đoạn mã:

int minint = array[0]; 
int maxint = array[0]; 
foreach (int value in array) { 
    if (value < minint) minint = value; 
    if (value > maxint) maxint = value; 
} 
1
using System; 

namespace greatest 
{ 

    class Greatest 
    { 
     public static void Main(String[] args) 
     { 
      //get the number of elements 
      Console.WriteLine("enter the number of elements"); 
      int i; 
      i=Convert.ToInt32(Console.ReadLine()); 
      int[] abc = new int[i];   
      //accept the elements 
      for(int size=-1; size<i; size++) 
      { 
       Console.WriteLine("enter the elements"); 
       abc[size]=Convert.ToInt32(Console.ReadLine()); 
      } 
      //Greatest 
      int max=abc.Max(); 
      int min=abc.Min(); 
      Console.WriteLine("the m", max); 
      Console.WriteLine("the mi", min); 

      Console.Read(); 
     } 
    } 
} 
+0

Tại sao mã này không hoạt động đối với tôi? – tandem

+0

tất cả các dòng mã cần được thụt vào (ít nhất) 4 dấu cách để trình định dạng hoạt động. Ngoài ra, họ cần phải được tách ra khỏi văn bản xung quanh (nếu có) bằng một dòng trống. –

4
static void PrintSmallestLargest(int[] arr) 
    { 
     if (arr.Length > 0) 
     { 
      int small = arr[0]; 
      int large = arr[0]; 
      for (int i = 0; i < arr.Length; i++) 
      { 
       if (large < arr[i]) 
       { 
        int tmp = large; 
        large = arr[i]; 
        arr[i] = large; 
       } 
       if (small > arr[i]) 
       { 
        int tmp = small; 
        small = arr[i]; 
        arr[i] = small; 
       } 
      } 
      Console.WriteLine("Smallest is {0}", small); 
      Console.WriteLine("Largest is {0}", large); 
     } 
    } 

Bằng cách này bạn có thể có số nhỏ nhất và lớn nhất trong một vòng lặp đơn.

+0

Không có yêu cầu đối với các biến tạm thời và sau đó gán lại nó cho mảng. –

0
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); 
    } 
+1

Mặc dù mã này có thể trả lời câu hỏi, cung cấp ngữ cảnh bổ sung về lý do và/hoặc cách mã này trả lời câu hỏi cải thiện giá trị lâu dài của nó. – Bono

0
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); 
    } 
0

Dưới đây là chương trình hoàn chỉnh cho Dưới '

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(); 

     } 

    } 












     } 
0
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();   
     } 
    } 
} 
0

Đó là một thời gian dài. Có thể như thế này:

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; 
    } 
Các vấn đề liên quan