java 鍵盤輸入使用循環遍歷數組方法實現求出輸入的最大值


public class Demo02ScannerSum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入第一個數字:");
        int a = sc.nextInt();
        System.out.println("請輸入第二個數字:");
        int b = sc.nextInt();
        System.out.println("請輸入第三個數字:");
        int c = sc.nextInt();
        int[] num = {a, b, c};
        int d = num.length;
        int max = num[0];
        for (int i = 0; i < d; i++) {
            if (num[i] > max) {
                max = num[i];
            }
        }
        System.out.println("最大值為" + max);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM