在 Java 中顯示三個整數的值的最大值
以下是顯示三個整數的值的最大值的示例。
示例
public class Demo {
public static void main(String[] args) {
int val1 = 10;
int val2 = 20;
int val3 = 30;
System.out.println("Number 1 = "+val1);
System.out.println("Number 2 = "+val2);
System.out.println("Number 3 = "+val3);
if (val2 > val1) {
val1 = val2;
}
if (val3 > val1) {
val1 = val3;
}
System.out.println("The greatest of three numbers: "+val1);
}
}輸出
Number 1 = 10 Number 2 = 20 Number 3 = 30 The greatest of three numbers: 30
在上面的程式中,我們生成了三個整數變數,它們將進行比較。
int val1 = 10; int val2 = 20; int val3 = 30;
現在使用條件來檢查哪個整數值最大。
if (val2 > val1) {
val1 = val2;
}
if (val3 > val1) {
val1 = val3;
}上面返回的是最大值。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP