สมมติว่าคุณต้องค้นหาองค์ประกอบในรายการต่อไปนี้ให้มากกว่า 80
int[] arr = new int[] {55, 100, 87, 45}; สำหรับสิ่งนั้น ให้วนซ้ำจนถึงความยาวอาร์เรย์ ที่นี่ res =80 คือองค์ประกอบที่กำหนด
for (int i = 0; i < arr.Length; i++) {
if(arr[i]<res) {
Console.WriteLine(arr[i]);
}
} ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = new int[] {
55,
100,
87,
45
};
// given integer
int res = 80;
Console.WriteLine("Given Integer {0}: ", res);
Console.WriteLine("Numbers larger than {0} = ", res);
for (int i = 0; i < arr.Length; i++) {
if (arr[i] > res) {
Console.WriteLine(arr[i]);
}
}
}
}
}