ก่อนอื่น มาตั้งค่าตัวเลขสามตัวกันก่อน −
int num1, num2, num3; // set the value of the three numbers num1 = 10; num2 = 20; num3 = 50;
ตรวจสอบหมายเลขแรกกับหมายเลขที่สอง ถ้า num1> num2 ให้ตรวจสอบ num1 ด้วย num3 หาก num1 มากกว่า num3 แสดงว่าจำนวนที่มากที่สุดคือ num1
ตัวอย่าง
คุณสามารถลองเรียกใช้รหัสต่อไปนี้เพื่อค้นหาตัวเลขสูงสุดสามตัว
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
int num1, num2, num3;
// set the value of the three numbers
num1 = 10;
num2 = 20;
num3 = 50;
if (num1 > num2) {
if (num1 > num3) {
Console.Write("Number one is the largest!\n");
} else {
Console.Write("Number three is the largest!\n");
}
}
else if (num2 > num3)
Console.Write("Number two is the largest!\n");
else
Console.Write("Number three is the largest!\n");
}
}
} ผลลัพธ์
Number three is the largest!