1、求圆的面积
public class Circel {
// 半径
double radius;
/*
* 测试类
*/
public static void main(String[] args) {
Circel circle = new Circel();
circle.radius = 5;
double area = circle.findArea();
System.out.println("半径为" + circle.radius + "的圆的面积为:" + area);
}
/**
* @Description 返回圆的面积的方法
* @author zhenghuhu
* @date 2021-7-10 9:13:29
* @return
*/
public double findArea() {
return Math.PI * radius * radius;
}
}
2、斐波那契数列
public class Fibonacci {
public static void main(String[] args) {
Fibonacci f = new Fibonacci();
System.out.println(f.fibonacciTest(7));
}
/**
* 求斐波那契数列某一位上的值
* @Description
* @author zhenghuhu
* @date 2021-7-10 11:23:17
* @param n 所求的斐波那契数列的位数
* @return 所求固定位置上的斐波那契数列的值
*/
public int fibonacciTest(int n) {
if (n == 1) {
return 1;
}else if (n == 2) {
return 1;
}
else {
return fibonacciTest(n - 1) + fibonacciTest(n - 2);
}
}
}
3、函数的递归调用
3.1、已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n), 其中n是大于0的整数,求f(10)的值。
public class MethodRecursion {
public static void main(String[] args) {
MethodRecursion methodRecursion = new MethodRecursion();
int fn = methodRecursion.f(10);
System.out.println(fn);
}
/**
* 递归调用实现函数的求解
* @Description 已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),
* 其中n是大于0的整数,求f(10)的值。
* @author zhenghuhu
* @date 2021-7-10 10:36:21
* @param n
* @return
*/
public int f(int n) {
if (n == 0) {
return 1;
}else if(n == 1) {
return 4;
}else {
return 2 * f(n -1 ) + f(n - 2);
}
}
}
3.2、已知一个数列:f(20) = 1,f(21) = 4,f(n+2) = 2*f(n+1)+f(n),其中n是大于0的整数,求f(10)的值。
public class MethodRecursion {
public static void main(String[] args) {
MethodRecursion methodRecursion = new MethodRecursion();
int fn2 = methodRecursion.f2(10);
System.out.println(fn2);
}
/**
*
* @Description 已知一个数列:f(20) = 1,f(21) = 4,f(n+2) = 2*f(n+1)+f(n),
* 其中n是大于0的整数,求f(10)的值。
* @author zhenghuhu
* @date 2021-7-10 10:44:35
* @param n
* @return
*/
public int f2(int n) {
if (n == 20) {
return 1;
}else if (n == 21) {
return 4;
}else {
return f(n + 2) - 2 * f(n + 1);
}
}
}
3.3、连续求和
public class Recursion {
public static void main(String[] args) {
Recursion recursion = new Recursion();
int sum = recursion.sum(100); // 求1~100的和
System.out.println(sum);
}
/**
* 连续求和
* @Description
* @author zhenghuhu
* @date 2021-7-10 9:45:26
* @param n
* @return
*/
public int sum(int n) {
if (n == 1) {
return 1;
}else {
return n + sum(n - 1);
}
}
}
3.4、求阶乘
public class Recursion {
public static void main(String[] args) {
Recursion recursion = new Recursion();
long product = recursion.product(10); // 求10的阶乘
System.out.println(product);
}
/**
* 求阶乘
* @Description
* @author zhenghuhu
* @date 2021-7-10 9:48:06
* @param n
* @return
*/
public long product(int n) {
if (n == 1) {
return 1;
}else {
return n*product(n - 1);
}
}
}
4、println的方法的重载
/**
*此题考察println的方法的重载
* @Description
* @author zhenghuhu
* @version
* @date 2021-7-7 20:55:32
*
*/
public class ArrayPrintTest {
ublic static void main(String[] args) {
int[] arr1 = new int[] {1, 2, 3, 4};
System.out.println(arr1);
System.out.println("arr1 = " + arr1);
char[] arr2 = new char[] {'a', 'b', 'c'};
System.out.println(arr2);
System.out.println("arr2 = " + arr2);
}
}
运行结果展示如下:
[I@15db9742
arr1 = [I@15db9742
abc
arr2 = [C@6d06d69c
5、数组换新
- 方式一:
/*
* 定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
* 让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的
* 新值。遍历新的数组。
*/
public class MicroSoftTest {
public static void main(String[] args) {
int[] arr = new int[]{12,3,3,34,56,77,432};
System.out.println(Arrays.toString(arr));
int temp = arr[0];
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] / temp;
}
System.out.println(Arrays.toString(arr));
}
}
- 方式二:
/*
* 定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
* 让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的
* 新值。遍历新的数组。
*/
public class MicroSoftTest {
public static void main(String[] args) {
int[] arr = new int[]{12,3,3,34,56,77,432};
System.out.println(Arrays.toString(arr));
for (int i = arr.length - 1; i >= 0 ; i--) {
arr[i] = arr[i] / arr[0];
}
System.out.println(Arrays.toString(arr));
}
}
6、数组元素的反转
- 方式一:
public class ArrayReverse {
public static void main(String[] args) {
String[] strs = new String[] {"WANG", "DA", "CHUAI", "JIU", "SHI", "WANG", "LI"};
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i] + "\t");
}
// 实现反转
for (int i = 0; i < strs.length / 2; i++) {
String temp = strs[i];
strs[i] = strs[strs.length - i -1];
strs[strs.length - i -1] = temp;
}
System.out.println("\n反转之后:");
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i] + "\t");
}
}
}
- 方式二:
public class ArrayReverse {
public static void main(String[] args) {
String[] strs = new String[] {"WANG", "DA", "CHUAI", "JIU", "SHI", "WANG", "LI"};
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i] + "\t");
}
// 实现反转
for (int i = 0, j = strs.length - 1; i < j; i++, j--) {
String temp = strs[i];
strs[i] = strs[j];
strs[j] = temp;
}
System.out.println("\n反转之后:");
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i] + "\t");
}
}
}