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!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP