练习汇总
数组练习
-
练习1:创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
public static void main(String[] args) { int[] array = new int[6]; for (int i = 0; i < 6; i++) { array[i] = (int)(Math.random() * 30) + 1; for (int j = 0; j < i; j++) { if (array[i] == array[j]) { i--; break; } } } for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } }
-
从键盘输入一个整数(1~20)
则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。例如: 输入数字2,则程序输出:
1 2
4 3
输入数字3,则程序输出:
1 2 3
8 9 4
7 6 5
输入数字4, 则程序输出:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数:"); int length = scanner.nextInt(); int [][] array = new int[length][length]; // k = 1表示向右;k = 2表示向下;K = 3表示向左;k = 4表示向上 int k = 1; int x = 0; int y = 0; for (int i = 1; i <= length * length; i++) { if (k == 1) { if (y < length && array[x][y] == 0) { array[x][y] = i; y++; } else { k = 2; x++; y--; i--; } } else if (k ==2) { if (x < length && array[x][y] == 0) { array[x][y] = i; x++; } else { k = 3; x--; y--; i--; } } else if (k ==3) { if (y >= 0 && array[x][y] == 0) { array[x][y] = i; y--; } else { k = 4; x--; y++; i--; } } else if (k ==4) { if (x >= 0 && array[x][y] == 0) { array[x][y] = i; x--; } else { k = 1; x++; y++; i--; } } } for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } }
-
使用简单数组
(1)创建一个名为ArrayTest的类,在main()方法中声明array1和array2两个变量,他们是int[]类型的数组。
(2)使用大括号{},把array1初始化为8个素数:2,3,5,7,11,13,17,19。
(3)显示array1的内容。
(4)赋值array2变量等于array1,修改array2中的偶索引元素,使其等于索引值(如array[0]=0,array[2]=2)。打印出array1。
思考:array1和array2是什么关系?
拓展:修改题目,实现array2对array1数组的复制public static void main(String[] args) { int[] array1, array2; array1 = new int[]{2, 3, 5, 7, 11, 13, 17, 19}; for (int i = 0; i < array1.length; i++){ System.out.print(array1[i] + "\t"); } System.out.println(); //array2 = array1;不能实现复制 //数组的复制 array2 = new int[array1.length]; for (int i = 0; i < array2.length; i++){ array2[i] = array1[i]; } for (int i = 0; i < array2.length; i += 2){ array2[i] = i; } for (int i = 0; i < array1.length; i++){ System.out.print(array1[i] + "\t"); } }
-
数组排序算法
/* 冒泡排序:由小到大排序 */ public static int[] bubbleSort(int[] array){ for (int i = 0; i < array.length; i++){ boolean flag = true; //标志位,若一轮下来没有元素被交换,则表示该序列已有序 for (int j = 0; j < array.length - i - 1; j++){ if (array[j] > array[j + 1]){ int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; flag = false; } } if (flage){ break; } } }
/* 快速排序 */ private static void swap(int[] data, int i, int j) { int temp = data[i]; data[i] = data[j]; data[j] = temp; } private static void subSort(int[] data, int start, int end) { if (start < end) { int base = data[start]; int low = start; int high = end + 1; while (true) { while (low < end && data[++low] - base <= 0) ; while (high > start && data[--high] - base >= 0) ; if (low < high) { swap(data, low, high); } else { break; } } swap(data, start, high); subSort(data, start, high - 1); subSort(data, high + 1, end); } } public static void quickSort(int[] data){ subSort(data,0,data.length-1); }
-
数组查找算法
/* 二分法查找 */ public static int binarySearch(int[] array, int dest){ int front = 0; int behind = array.length - 1; int mid; while (front <= behind){ mid = (front + behind) / 2; if (dest > array[mid]){ front = mid + 1; } else if (dest < array[mid]){ behind = mid - 1; } else if (dest == array[mid]) { return mid; } } return -1; }
对象数组练习
-
定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息public class Exercise4 { public static void main(String[] args) { Student[] students = new Student[20]; Exercise4 demo = new Exercise4(); demo.initStudent(students, 20); demo.searchState(students, 3); demo.sortWithScore(students); demo.printInfo(students); } /** * * @Description 初始化对象数组 * @author zbs * @date Jul 29, 20212:36:24 PM * @param students */ public void initStudent(Student[] students, int num) { for (int i = 0; i < num; i++) { students[i] = new Student(); students[i].number = i + 1; students[i].score = (int)(Math.random() * 100) + 1; students[i].state = (int)(Math.random() * 6) + 1; } } /** * * @Description 根据年级进行查找 * @author zbs * @date Jul 29, 20212:37:15 PM * @param students * @param state */ public void searchState(Student[] students, int state) { System.out.println("三年级的学生有:"); System.out.println("学号\t成绩\t年级"); for (int i = 0; i < students.length; i++) { if (students[i].state == state) { students[i].showInfo(); } } } /** * * @Description 根据成绩进行排序 * @author zbs * @date Jul 29, 20212:37:43 PM * @param students */ public void sortWithScore(Student[] students) { boolean flag; for (int i = 0; i < students.length; i++) { flag = true; for (int j = 0; j < students.length - i - 1; j++) { if (students[j].score > students[j + 1].score) { Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; flag = false; } } if (flag) { break; } } } /** * * @Description 输出对象数组的信息 * @author zbs * @date Jul 29, 20212:38:18 PM * @param students */ public void printInfo(Student[] students) { System.out.println("按成绩由小到大的排序为:"); System.out.println("学号\t成绩\t年级"); for (int i = 0; i < students.length; i++) { students[i].showInfo(); } } } class Student{ /** * 学号 */ int number; /** * 年级 */ int state; /** * 分数 */ int score; public void showInfo() { System.out.print(number + "\t"); System.out.print(score + "\t"); System.out.println(state + "\t"); } }
小实验1
-
自定义数组工具类
public class ArrayUtil { /** *返回数组的最大值ֵ * @Description * @author zbs * @date Jul 29, 20214:25:45 PM * @param array * @return */ public static int getMax(int[] array) { int max = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; } /** * 返回数组的最小值ֵ * @Description * @author zbs * @date Jul 29, 20214:29:13 PM * @param array * @return */ public static int getMin(int[] array) { int min = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] < min) { min = array[i]; } } return min; } /** *返回数组的总和 * @Description * @author zbs * @date Jul 29, 20214:34:02 PM * @param array * @return */ public static int getSum(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum; } /** * 返回数组的平均值ֵ * @Description * @author zbs * @date Jul 29, 20214:37:43 PM * @param array * @return */ public static double getAvarage(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum/array.length; } /** * 反转数组 * @Description * @author zbs * @date Jul 29, 20214:41:50 PM * @param array */ public static void reverse(int[] array) { for (int i = 0; i < array.length / 2; i++) { int index = array.length - 1 - i; int temp = array[i]; array[i] = array[index]; array[index] = temp; } } /** *复制数组 * @Description * @author zbs * @date Jul 29, 20214:44:02 PM * @param array * @return */ public static int[] copy(int[] array) { int[] temp = new int[array.length]; for (int i = 0; i < array.length; i++) { temp[i] = array[i]; } return temp; } /** * 冒泡排序 * @Description * @author zbs * @date Jul 29, 20214:48:23 PM * @param array */ public static void bullotSort(int[] array) { boolean flag; for (int i = 0; i < array.length - 1; i++) { flag = true; for (int j = 0; j < array.length - 1 - i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; flag = false; } } if (flag) { break; } } } /** * 输出数组内容 * @Description * @author zbs * @date Jul 29, 20214:50:24 PM * @param array */ public static void toString(int[] array) { System.out.print("["); for (int i = 0; i < array.length - 1; i++) { System.out.print(array[i] + ","); } System.out.print(array[array.length - 1] + "]"); } /** * 查找指定元素的位置 * @Description * @author zbs * @date Jul 29, 20214:52:35 PM * @param array * @param dest * @return */ public static int getElem(int[] array, int dest) { for (int i = 0; i < array.length; i++) { if (array[i] == dest) { return i; } } return -1; } } public class ArrayUtilTest { public static void main(String[] args) { int[] array = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}; ArrayUtil.bullotSort(array); ArrayUtil.toString(array); } }
构造器练习
-
UML类图相关知识
-
public class Test{ public static void main(String[] args) { Boy boy = new Boy("zbs", 20); Girl girl = new Girl("lhp", 19); boy.marry(girl); girl.marry(boy); } } class Boy{ private String name; private int age; public Boy(){ } public Boy(String name){ this.name = name; } public Boy(int age){ this.age = age; } public Boy(String name, int age){ this.name = name; this.age = age; } public void setName(String i){ name = i; } public String getName(){ return this.name; } public void setAge(int i){ age = i; } public int getAge(){ return this.age; } public void marry(Girl girl){ System.out.println("我想娶" + girl.getName()); } public void shout(){ if (this.age >= 22){ System.out.println("可以去结婚了"); } else { System.out.println("再等等吧"); } } } class Girl{ private String name; private int age; public Girl(){ } public Girl(String name){ this.name = name; } public Girl(int age){ this.age = age; } public Girl(String name, int age){ this.name = name; this.age = age; } public void setName(String i){ name = i; } public String getName(){ return this.name; } public void marry(Boy boy){ System.out.println("我想嫁给" + boy.getName()); boy.marry(this); } /** * @Description 比较两个对象的大小 * @author zbs * @date Jul 29, 20212:38:18 PM * @param girl * @return 正数:当前对象大;负数:当前对象小;0:当前对象与形参对象相等 */ public void compare(Girl girl){ return this.age - girl.age; } }
小实验2
-
根据下列要求书写代码:
-
按照如下的 UML 类图,创建相应的类,提供必要的结构
在提款方法 withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。deposit()方法表示存款。
-
按照如下的 UML 类图,创建相应的类,提供必要的结构
-
按照如下的 UML 类图,创建相应的类,提供必要的结构
-
addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer 对象,然后把它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1。
-
getNumOfCustomers 方法返回 numberofCustomers 属性值。
-
getCustomer 方法返回与给出的 index 参数相关的客户。
-
-
创建 BankTest 类,进行测试。
public class Account{ private double balance; public Account(double init_balance){ this.balance = init_balance; } public double getBalance(){ return this.balance; } public void deposit(double amt){ if (amt >= 0){ balance += amt; sysout("存钱成功!"); } else { sysout("存钱数不能为负数!!"); } } public void withdraw(double amt){ if (balance >= amt){ balance -= amt; sysout("取钱成功!"); } else { sysout("余额不足,取钱失败!"); } } }
public class Customer{ private String firstName; private String lastName; private Account account; public Customer(String f, String l){ this.firstName = f; this.lastName = l; } public String getFirstName(){ return this.firstName; } public String getLastName(){ return this.lastName; } public Account getAccount(){ return this.account; } public void setAccount(Account acct){ this.account = acct; } }
public class Bank{ private Customer[] customers; private int numberOfCustomer; public Bank(){ customers = new Customer[10]; } public addCustomer(String f, String l){ customers[numberOfCustomer++] = new Custemou(f, l); } public int getNumOfCustomers(){ return this.numberOfCustomer; } public Customer getCustomer(int index){ if (index < numberOfCustomer && index >= 0){ return this.customers[index]; } return null; } }
public class Test{ public static void main(String[] args){ Bank bank = new Bank(); bank.addCustomer("zhao", "binsheng"); bank.getCustomer(0).setAccount(new Account(2000)); bank.getCustomer(0).getAccount().withdraw(500); double balance = bank.getCustomer(0).getAccount().getBalance(); System.out.println("客户:" + bank.getCustomer(0).getFirstName() + "的账户余额为:" + balance); } }
-