import java.util.Scanner;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? Scanner input = new Scanner(System.in);
? ? ? ? System.out.print("請(qǐng)輸入第一個(gè)整數(shù):");
? ? ? ? int a = input.nextInt();
? ? ? ? System.out.print("請(qǐng)輸入第二個(gè)整數(shù):");
? ? ? ? int b = input.nextInt();
? ? ? ? System.out.print("請(qǐng)輸入第三個(gè)整數(shù):");
? ? ? ? int c = input.nextInt();
? ? ? ? int max = a;
? ? ? ? if (b > max) {
? ? ? ? ? ? max = b;
? ? ? ? }
? ? ? ? if (c > max) {
? ? ? ? ? ? max = c;
? ? ? ? }
? ? ? ? System.out.println("最大的數(shù)是:" + max);
? ? }
}
?
在這個(gè)程序中,我們首先創(chuàng)建了一個(gè) Scanner
對(duì)象用于從控制臺(tái)讀取輸入。然后依次讀取了三個(gè)整數(shù),并將它們分別存儲(chǔ)在變量 a
、b
和 c
中。
接下來(lái),我們使用一個(gè) if
語(yǔ)句鏈,比較這三個(gè)數(shù)的大小,找出最大的那個(gè)數(shù),并將其存儲(chǔ)在變量 max
中。
最后,我們使用 System.out.println()
方法打印出最大的數(shù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-480333.html
2.以下是另外一種實(shí)現(xiàn),也可以用于從控制臺(tái)讀取三個(gè)整數(shù),并輸出其中最大的數(shù):
import java.util.Arrays;
import java.util.Scanner;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? Scanner input = new Scanner(System.in);
? ? ? ? int[] nums = new int[3];
? ? ? ? for (int i = 0; i < 3; i++) {
? ? ? ? ? ? System.out.print("請(qǐng)輸入第" + (i+1) + "個(gè)整數(shù):");
? ? ? ? ? ? nums[i] = input.nextInt();
? ? ? ? }
? ? ? ? int max = Arrays.stream(nums).max().getAsInt();
? ? ? ? System.out.println("最大的數(shù)是:" + max);
? ? }
}
?
在這個(gè)程序中,我們首先創(chuàng)建了一個(gè) Scanner
對(duì)象用于從控制臺(tái)讀取輸入。然后使用一個(gè)循環(huán),依次讀取了三個(gè)整數(shù),并將它們存儲(chǔ)在一個(gè)數(shù)組 nums
中。
接下來(lái),我們使用 Arrays.stream()
方法將數(shù)組轉(zhuǎn)換為一個(gè)流,然后使用 max()
方法找出其中的最大值,并將其存儲(chǔ)在變量 max
中。
最后,我們使用 System.out.println()
方法打印出最大的數(shù)。
以下是另外一種實(shí)現(xiàn),也可以用于從控制臺(tái)讀取三個(gè)整數(shù),并輸出其中最大的數(shù):
import java.util.Scanner;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? Scanner input = new Scanner(System.in);
? ? ? ? System.out.print("請(qǐng)輸入第一個(gè)整數(shù):");
? ? ? ? int a = input.nextInt();
? ? ? ? System.out.print("請(qǐng)輸入第二個(gè)整數(shù):");
? ? ? ? int b = input.nextInt();
? ? ? ? System.out.print("請(qǐng)輸入第三個(gè)整數(shù):");
? ? ? ? int c = input.nextInt();
? ? ? ? int max = Math.max(Math.max(a, b), c);
? ? ? ? System.out.println("最大的數(shù)是:" + max);
? ? }
}
在這個(gè)程序中,我們首先創(chuàng)建了一個(gè) Scanner
對(duì)象用于從控制臺(tái)讀取輸入。然后依次讀取了三個(gè)整數(shù),并將它們分別存儲(chǔ)在變量 a
、b
和 c
中。
接下來(lái),我們使用 Math.max()
方法比較這三個(gè)數(shù)的大小,找出最大的那個(gè)數(shù),并將其存儲(chǔ)在變量 max
中。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-480333.html
最后,我們使用 System.out.println()
方法打印出最大的數(shù)。
到了這里,關(guān)于用java任意輸入三個(gè)整數(shù),并將最大輸出的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!