博客
关于我
长春大学20级十一周第一次上机(5月10号)
阅读量:796 次
发布时间:2023-03-25

本文共 3876 字,大约阅读时间需要 12 分钟。

(一)计算数组中奇偶数和并排序

class Tool {
int[] arr = {8, 54, 3, 47, 148, 77, 56, 55, 20};
public void countSum(int[] array) {
int oddSum = 0;
int evenSum = 0;
for (int num : array) {
if (num % 2 == 0) {
evenSum += num;
} else {
oddSum += num;
}
}
System.out.println("奇数和为:" + oddSum + "\n" + "偶数和为:" + evenSum);
}
public void sort(int[] array) {
// 实现冒泡排序,从大到小排序
for (int i = 0; i < array.length - 1; i++) {
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;
}
}
}
System.out.println("排序后的数组为:" + Arrays.toString(array));
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {8, 54, 3, 47, 148, 77, 56, 55, 20};
Tool tool = new Tool();
tool.countSum(arr);
tool.sort(arr);
}
}

(二)接口与圆柱体计算

interface Computer {
int compute(int num1, int num2);
}
class Add implements Computer {
@Override
public int compute(int num1, int num2) {
return num1 + num2;
}
}
class Subtract implements Computer {
@Override
public int compute(int num1, int num2) {
return num1 - num2;
}
}
class Multiply implements Computer {
@Override
public int compute(int num1, int num2) {
return num1 * num2;
}
}
class Divide implements Computer {
@Override
public int compute(int num1, int num2) {
if (num2 == 0) {
System.out.println("除数不能为零");
return 0;
}
return num1 / num2;
}
}
class Calculator {
public void performCalculations(Computer operation, int a, int b) {
operation.compute(a, b);
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.performCalculations(new Add(), 10, 5);
calc.performCalculations(new Subtract(), 20, 10);
calc.performCalculations(new Multiply(), 3, 4);
calc.performCalculations(new Divide(), 8, 2);
}
}

(三)工具类设计

class Tool {
int[] numbers = {8, 54, 3, 47, 148, 77, 56, 55, 20};
void countSum() {
int evenSum = 0;
int oddSum = 0;
for (int num : numbers) {
if (num % 2 == 0) {
evenSum += num;
} else {
oddSum += num;
}
}
System.out.println("偶数和为:" + evenSum + "\n奇数和为:" + oddSum);
}
void sortArray() {
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = 0; j < numbers.length - 1 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println("排序后的数组:" + Arrays.toString(numbers));
}
}
public class Main {
public static void main(String[] args) {
Tool tool = new Tool();
tool.countSum();
tool.sortArray();
}
}

(四)冒泡排序实现

class SortingTool {
int[] array = {3, 6, 2, 8, 1};
void sort() {
for (int i = 0; i < array.length - 1; i++) {
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;
}
}
}
System.out.println("排序后的数组:" + Arrays.toString(array));
}
}
public class Main {
public static void main(String[] args) {
SortingTool tool = new SortingTool();
tool.sort();
}
}

转载地址:http://mkhfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现Exceeding words超词(差距是ascii码的距离) 算法(附完整源码)
查看>>
Objective-C实现extended euclidean algorithm扩展欧几里得算法(附完整源码)
查看>>
Objective-C实现Factorial digit sum阶乘数字和算法(附完整源码)
查看>>
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>
Objective-C实现FigurateNumber垛积数算法(附完整源码)
查看>>
Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
查看>>
Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
查看>>
Objective-C实现hamming numbers汉明数算法(附完整源码)
查看>>
Objective-C实现hanning 窗(附完整源码)
查看>>
Objective-C实现hanoiTower汉诺塔算法(附完整源码)
查看>>
Objective-C实现hardy ramanujana定理算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现hornerMethod霍纳法算法(附完整源码)
查看>>
Objective-C实现Http Post请求(附完整源码)
查看>>
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>
Objective-C实现IIR数字滤波器(附完整源码)
查看>>
Objective-C实现insertion sort插入排序算法(附完整源码)
查看>>