⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 salessummary.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:
public class SalesSummary {
  final static String[] CATEGORIES = {
      "Toys", "Clothes", "Educational"};
  final static String[] REGIONS = {
      "North", "South"};

  public static double sumOfRow(double matrix[][], int row) {
    double sum = 0.0;
    for (int col = 0; col < matrix[0].length; col++) {
      sum += matrix[row][col];
    }
    return sum;
  }

  public static double sumOfCol(double matrix[][], int col) {
    double sum = 0;
    for (int row = 0; row < matrix.length; row++) {
      sum += matrix[row][col];
    }
    return sum;
  }

  public static void showMatrix(double matrix[][]) {
    System.out.print("\t");
    for (int i = 0; i < CATEGORIES.length; i++) {
      System.out.print(CATEGORIES[i] + "\t");
    }
    System.out.println();
    for (int row = 0; row < matrix.length; row++) {
      System.out.print(REGIONS[row] + ":\t");
      for (int col = 0; col < matrix[row].length; col++) {
        System.out.print(matrix[row][col] + "\t");
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }

  public static void main(String args[]) {
    double table[][] = new double[REGIONS.length][CATEGORIES.length];
    for (int row = 0; row < table.length; row++) {
      for (int col = 0; col < table[row].length; col++) {
        table[row][col] = 1000 * Math.random();
      }
    }
    showMatrix(table);
    for (int region = 0; region < REGIONS.length; region++) {
      System.out.println("Sales in " + REGIONS[region] + ": " +
                         sumOfRow(table, region));
    }
    for (int cat = 0; cat < CATEGORIES.length; cat++) {
      System.out.println("Sales in " + CATEGORIES[cat] + ": " +
                         sumOfCol(table, cat));
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -