C# 程式查詢三個數中的最大值
首先,讓我們設定這三個數 −
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!
"); } else { Console.Write("Number three is the largest!
"); } } else if (num2 > num3) Console.Write("Number two is the largest!
"); else Console.Write("Number three is the largest!
"); } } }
輸出
Number three is the largest!
廣告