ขั้นแรก ประกาศและเริ่มต้นสองหมายเลข
int num1 = 50; int num2 = 90;
จากนั้นใช้ if-else เพื่อค้นหาจำนวนสูงสุด
if (num1 > num2) {
maxNum = num1;
} else {
maxNum = num2;
} ด้านบน เราได้ตั้งค่าสูงสุดเป็นตัวแปร maxNum และพิมพ์ในภายหลัง
ต่อไปนี้คือตัวอย่างที่สมบูรณ์เพื่อค้นหาตัวเลขสูงสุดระหว่าง 2 ตัวใน C#
ตัวอย่าง
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int num1 = 50;
int num2 = 90;
int maxNum;
Console.WriteLine("Number 1: "+num1);
Console.WriteLine("Number 2: "+num2);
if (num1 > num2) {
maxNum = num1;
} else {
maxNum = num2;
}
Console.WriteLine("Maximum number is: "+maxNum);
Console.ReadKey() ;
}
}
} ผลลัพธ์
Number 1: 50 Number 2: 90 Maximum number is: 90