Discuss / Java / 交作业

交作业

Topic source

Awdcuhhk

#1 Created at ... [Delete] [Delete and Lock User]
package com.itranswarp.learnjava;

import java.util.Arrays;

/**
 * 遍历数组
 */
public class Main {
	public static void main(String[] args) {
		int[] ns = { 1, 4, 9, 16, 25 };
		// 打印倒序后的数组:
		System.out.println(Arrays.toString(reverse(ns)));
	}

	// 反转数组的方法
	public static int[] reverse(int[] arrays) {
		int[] result = new int[arrays.length];
		// 反转的具体实现:
		for (int i = 0, j = result.length - 1; i < arrays.length; i++, j--) {
			result[j] = arrays[i];
		}
		return result;
	}

}

Awdcuhhk

#2 Created at ... [Delete] [Delete and Lock User]

上面的是前一页的,下面的才是这次的。

package com.itranswarp.learnjava;

import java.util.Arrays;

/**
 * 降序排序
 */
public class Main {
	public static void main(String[] args) {
		int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
		// 排序前:
		System.out.println(Arrays.toString(ns));
		// TODO:
		for (int i = 0; i < ns.length - 1; i++) {
			for (int j = 0; j < ns.length - i - 1; j++) {
				if (ns[j] < ns[j + 1]) {
					int temp = ns[j];
					ns[j] = ns[j + 1];
					ns[j + 1] = temp;
				}
			}
		}
		// 排序后:
		System.out.println(Arrays.toString(ns));
		if (Arrays.toString(ns).equals("[96, 89, 73, 65, 50, 36, 28, 18, 12, 8]")) {
			System.out.println("测试成功");
		} else {
			System.out.println("测试失败");
		}
	}
}


  • 1

Reply