※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

69 瀏覽人次

【程式語言】JAVA的第五堂課 |在陣列中取最大值、加總

Published: in JAVA by .

public class hw5 {
    public static void main(String[] args) {
        int price[]={12,16,10,14,15}; 
        int sale[][] = {{33,32,56,45,33},
                        {77,33,68,45,23},
                        {43,55,43,67,65}};
        int money[] = new int[3];
        int money_pd[] = new int[5];
        System.out.println("銷售員\t產品A\t產品B\t產品C\t產品D\t產品E");
        for (int i = 0 ; i < sale.length ; i++ ) {
            System.out.print((i+1) + "\t");
            for (int j = 0 ; j < sale[1].length ; j++) {
                System.out.print(sale[i][j] + "\t");
                switch (j){
                    case 0:
                        money_pd[0] += price[j] * sale[i][j];
                        break;
                    case 1:
                        money_pd[1] += price[j] * sale[i][j];
                        break;
                    case 2:
                        money_pd[2] += price[j] * sale[i][j];
                        break;
                    case 3:
                        money_pd[3] += price[j] * sale[i][j];
                        break;
                    case 4:
                        money_pd[4] += price[j] * sale[i][j];
                        break;
                }
                if (i == 0) {
                    money[i] += price[j] * sale[i][j];
                } 
                else if (i == 1) {
                    money[i] += price[j] * sale[i][j];
                } 
                else{
                    money[i] += price[j] * sale[i][j];
                }
            }
            System.out.println();
        } 
        int best = 0 , index = 0 , best_pd = 0;
        for (int k = 0 ; k < money.length ; k++) {
            if (money[k] > best) {
                best += 1;
            }
        }

        for (int l = 0 ; l < money_pd.length ; l++) {
            if (money_pd[l] > best_pd) {
                best_pd = money_pd[l];
                index += 1;
            }
        }

        System.out.println("(1)------------");
        System.out.print("|銷售員1銷售金額為:" + money[0] + " |\n|銷售員2銷售金額為:" + money[1] + " |\n|銷售員3銷售金額為:" + money[2] + " |\n");
        System.out.println("(2)------------");
        System.out.print("|產品 A 總金額為:" + money_pd[0]  + "   |\n|產品 B 總金額為:" + money_pd[1] +"   |\n|產品 C 總金額為:" + money_pd[2] + "   |\n|產品 D 總金額為:" +
                        money_pd[3] + "   |\n|產品 E 總金額為:" + money_pd[4] + "   |\n");
        System.out.println("(3)------------");
        System.out.print("|賣最多的銷售員為: " + best + "    |\n");
        System.out.println("(4)------------");
        System.out.print("|賣最多的商品為:   " + ((char)(index+65)) + "    |\n");
        System.out.println("--------------");
    }

}

Result:

©2019 - 2024 Henry Chen