Java 数组/对象练习一则

以下所有代码都是:建一个Hello类,把代码拷贝进去,就可以运行了~

1.反转数组:

public class Hello {

    public static int[] reverse(int[] args) {
        for (int i=0; i<args.length; i++) {
            args[i] = args.length-i;
        }
        return args;
    }
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        array = reverse(array);
        for (int i=0; i<array.length; i++) {
            System.out.print(array[i] + "	");
        }
    }
}

运行结果:

写法二(原理与写法一同):

public class Hello {
    public static void reverse(int[] args) {
        for (int i = 0; i < args.length; i++) {
            args[i] = args.length-i;
            System.out.print(args[i] + "	");
        }
    }
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        reverse(array);
    }
}

运行结果:

写法三:

public class Hello {

    public static int[] reverse(int[] args) {
        for (int i=0; i<(args.length)/2; i++) {
            int temp =args[i];
            args[i] = args[args.length-i-1];
            args[args.length-i-1] = temp;
        }
        return args;
    }
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        array = reverse(array);
        for (int i=0; i<array.length; i++) {
            System.out.print(array[i] + "	");
        }
    }
}

运行结果:

2.随机数组求和写法已更正

public class Hello {
    public static void randomArray() {
        int[][] array = new int[3][3];
        int allSum = 0;
        for (int i = 0; i < array.length; i++) {
            int rowSum = 0;
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = (int)(Math.random()*50);
                rowSum += array[i][j];
            }
            allSum += rowSum;
            System.out.println((i+1) + "行总和是: " + rowSum);
            System.out.println((i+1) + "行平均是: " + rowSum/(array[i].length+0.0));
        }
        System.out.println("全部总和是" + allSum);
    }
    public static void main(String[] args) {
        randomArray();
    }
}

运行结果:

写法二(同上+优化小数点位数):

import java.text.DecimalFormat;

public class Hello {
    public static void randomArray() {
        int[][] array = new int[3][3];
        DecimalFormat df = new DecimalFormat(".00");
        for (int i = 0; i < array.length; i++) {
            int rowSum = 0;
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = (int)(Math.random()*50);
                rowSum += array[i][j];
            }
            System.out.println("第" + (i+1) + "行总和是: " + rowSum);
            double average = (rowSum/(array[i].length+0.0));
            System.out.println("第" + (i+1) + "行平均是: " + df.format(average));
        }
    }
    public static void main(String[] args) {
        randomArray();
    }
}

运行结果:

3.盒子类


class Box {
	
	// 定义宽高长属性
	int width;
	int height;
	int length;
	
	// 构造函数
	Box(){}
	Box(int width, int height, int length) {
		this.width = width;
		this.height = height;
		this.length = length;
	}
	
	// 计算体积
	public int volume() {
		int result = this.width * this.width * this.height;
		return result;
	}
	
	// 计算面积
	public int area() {
		int result = 2*(
				this.length * this.width
				+ this.length * this.height
				+ this.width * this.height
				);
		return result;
	}
}

public class Hello {

	public static void main(String[] args) {
		Box box = new Box(3, 5, 8);
		int volume = box.volume();
		int area = box.area();
		System.out.println("面积是:" + area);
		System.out.println("体积是:" + volume);
	}
}

运行结果:

4.时间类

class Time {
    private int hour;  // 小时
    private int minute;  // 分钟
    private int second;  // 秒

    // 构造器
    public Time(){}
    public Time(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    // 打印时间方法
    public void printTime() {
        System.out.println(
                "小时: " + this.hour
                        + "	分钟: " + this.minute
                        + "	秒: " + this.second
        );
    }

    // getter & settter method
    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }
}

public class Hello {
    public static void main(String[] args) {
        Time time = new Time(20, 21, 23);
        time.printTime();
    }
}

运行结果:

原文地址:https://www.cnblogs.com/amnotgcs/p/13865912.html