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!

更新於:2020 年 6 月 19 日

1.2 萬+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.