java作业代码的分析

以下是某java老师留的很简单的作业,以下是添加了注释的代码。

1.编程对10个整数进行排序。

import java.util.Scanner;

public class work1 {

    public static void main(String[] args) {
        int array[] = new int[10];
        Scanner s = new Scanner(System.in);
        for (int i = 0; i < 10; i++) { // 提示要输入的数字
            System.out.println("input " + (i + 1) + " number.");
            array[i] = s.nextInt();
        }

        for (int i = 0; i < 9; i++) {  // 冒泡排序
            for (int j = 0; j < (9 - i); j++) {
                if (array[j] < array[j + 1]) {
                    int temp = 0;
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < 10; i++) {  // 排序结果输出
            System.out.print(array[i] + " ");
        }
    }
}

反编译的java代码:

import java.io.PrintStream;
import java.util.Scanner;

public class work1
{
  public static void main(String[] paramArrayOfString)
  {
    int[] arrayOfInt = new int[10];
    Scanner localScanner = new Scanner(System.in);
    for (int i = 0; i < 10; i++) {
      System.out.println("input " + (i + 1) + " number.");
      arrayOfInt[i] = localScanner.nextInt();
    }

    for (i = 0; i < 9; i++) {
      for (int j = 0; j < 9 - i; j++) {
        if (arrayOfInt[j] < arrayOfInt[(j + 1)]) {
          int k = 0;
          k = arrayOfInt[j];
          arrayOfInt[j] = arrayOfInt[(j + 1)];
          arrayOfInt[(j + 1)] = k;
        }
      }
    }
    for (i = 0; i < 10; i++)
      System.out.print(arrayOfInt[i] + " ");
  }
}

=======================================

2.求一个10行、10列整型方阵对角线上元素之积。

public class work3 {

    public static void main(String[] args) {
        int array[][] = new int[10][10]; // 自动填充数组
        int add = 1;
        int sum = 1;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                array[i][j] = add;
                add += 1;
            }
        }
        
        for (int i = 0; i < 10; i++) { //求对角线的积,并输出结果
            sum *= array[i][i];
        }
        System.out.println(sum);
    }
}

反编译的java代码:

import java.io.PrintStream;

public class work3
{
  public static void main(String[] paramArrayOfString)
  {
    int[][] arrayOfInt = new int[10][10];
    int i = 1;
    int j = 1;
    for (int k = 0; k < 10; k++) {
      for (int m = 0; m < 10; m++) {
        arrayOfInt[k][m] = i;
        i++;
      }
    }

    for (k = 0; k < 10; k++) {
      j *= arrayOfInt[k][k];
    }
    System.out.println(j);
  }
}

========================================

3.编写程序,计算一维数组中的最大值与其所在位置。

import java.util.*;

public class work5 {
    public static void main(String[] args) {
        int array[] = new int[10]; // 输入的数据存入此数组
        int root[] = new int[10]; // 标记数组,初始化为0,如果对应array中的数是最大值,则标记为1
        Scanner x = new Scanner(System.in);

        for (int i = 0; i < 10; i++) { // 输入数据
            System.out.println("please input the " + (i + 1) + " number.");
            array[i] = x.nextInt();
        }
        for (int i = 0; i < 10; i++) { // 输出刚才输入的数据
            System.out.print(array[i] + " ");
        }
        int max = 0;
        for (int i = 0; i < 10; i++) { // 找出最大的数
            if (array[i] > max) {
                max = array[i];
            }
        }
        for (int i = 0; i < 10; i++) { // 标记最大值所对应的下标
            if (max == array[i]) {
                root[i] = 1;
            }
        }
        System.out.println(" ");
        System.out.println("the max number is " + max); // 输出最大值
        System.out.print("where the max number is:");
        for (int i = 0; i < 10; i++) { // 输出最大值的位置
            if (1 == root[i]) {
                System.out.print((i + 1) + " ");
            }
        }
        System.out.println(" ");
    }
}

反编译的java代码:

import java.io.PrintStream;
import java.util.Scanner;

public class work5
{
  public static void main(String[] paramArrayOfString)
  {
    int[] arrayOfInt1 = new int[10];
    int[] arrayOfInt2 = new int[10];
    Scanner localScanner = new Scanner(System.in);

    for (int i = 0; i < 10; i++) {
      System.out.println("please input the " + (i + 1) + " number.");
      arrayOfInt1[i] = localScanner.nextInt();
    }

    for (i = 0; i < 10; i++) {
      System.out.print(arrayOfInt1[i] + " ");
    }
    i = 0;
    for (int j = 0; j < 10; j++) {
      if (arrayOfInt1[j] > i) {
        i = arrayOfInt1[j];
      }
    }

    for (j = 0; j < 10; j++) {
      if (i == arrayOfInt1[j]) {
        arrayOfInt2[j] = 1;
      }
    }

    System.out.println(" ");

    System.out.println("the max number is " + i);
    System.out.print("where the max number weizhi is:");
    for (j = 0; j < 10; j++) {
      if (1 == arrayOfInt2[j]) {
        System.out.print(j + 1 + " ");
      }
    }
    System.out.println(" ");
  }
}

===============================

以上就是未经任何优化的代码,嗯……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值