JAVA学习——函数总结

  • Post author:
  • Post category:java




记住格式

                //拿来一个扫描器:
                Scanner sc = new Scanner(System.in);
                //给一个友好性的提示:
                System.out.print("请录入一个半径:");
                //让扫描器扫描键盘录入的int类型的数据:
                int r = sc.nextInt();
                
double[] score = new double[10];
		double sum=0, max = 0,min =0;
		Scanner sc = new Scanner(System.in);  //ctr+shift+o导包;alt+/代码提示;ctr+alt+上下键复制
		for(int i =0;i<score.length;i++) {
			System.out.print("请输入第"+(i+1)+"个同学的成绩:");
			score[i] = sc.nextDouble();

//string两种写法

//import java.util.Scanner;
class TestWork604{
	public static void main(String[] args){
		/**
		Scanner sc = new Scanner(System.in);
		String s = sc.nextline();
		System.out.println(s);
		**/
		String str = "123454321";
		str=str.trim();
		boolean flag=isPalindrome(str,0,str.length()-1);
		System.out.println("Flag is: "+flag);

		
	}

//spit放入数组

在这里插入图片描述



正文

在这里插入图片描述

在这里插入图片描述

/**

1.java中函数是属于类(class)的,必须定义在类中

2.函数无法自己执行,必须通过调用才可以执行

3.java中:静态方法没有办法直接调用非静态方法,只能调用静态方法

4.有参数的函数,调用时必须传递对应的参数

5.函数中,遇到return则立即结束,并且返回

**/



一.函数定义

/**

java定义函数:

访问修饰符 函数返回子类型|void 函数名称([参数列表]){


函数体;

}

**/



二.函数调用

/**

函数调用:

函数名称([参数列表]);


重点内容:函数在内存的调用本质

栈:先进后出

队列:先进先出

执行栈:

函数怎么调用

局部变量

/**

局部变量(本地变量):定义在函数中,作用域是整个函数类,无法跨越

成员变量:在class中 ,作用域是整个类

**/

在这里插入图片描述



三.函数分类

/**

函数分类:

有没有参数:

有参数|无参数(未知输入数据;更加灵活)

有没有返回值:

有返回值|无返回值

定义者:

系统函数|第三者|自定义函数

**/



四.代码

//1.函数定义

//1.函数定义
class TestFun{
	public static void main(String[] args){
		//函数调用
		sayHello ();
		
		//注意,在main函数中,如果要调用非静态方法
		//必须使用构建对象的方式(面向对象)
		//TestFun tf = new TestFun();
		//tf.sayHello;  
	}
	
	public static void sayHello(){
		System.out.println("hello, i am function...");
	}
	
}


//2.函数分类

//2.函数分类01
class TestFun{
	public static void main(String[] args){
		//函数分类
		showInfo ('刘莹',20,‘女’);  
	}
	
	public static void showInfo (String name, int age,char gender)//形参{
		System.out.println("name:" + name);
		System.out.println("age:" + age);
		System.out.println("gender:" + gender);
	}
	
}

//2.函数分类02(返回值)
class TestFun{
	public static void main(String[] args){
		//将函数的返回值使用变量接受,形参与实参数据类型一致或者强制转换
		int a = add(2,8);
		System.out.println("2 + 8 =" + a);
	}
	
	public static int add(int x,int y) { //没有void则有返回值 
		return x + y;
	}
	
}




//函数判断一个数是不是素数

//函数判断一个数是不是素数
import java.util.Scanner;

public static void main(String[] args){
	Scanner sc = new Scanner(Systeam.in);
	System.out.print(">");
	int num = sc.nextInt();
	
	boolean Primer = isPrime(num);
	if(Primer){
		System.out.print("是质数");
	}else{
		System.out.print("不是质数");
	}
}

public static boolean isPrimer(int num){
	for(int i =2;i <num / 2 ;++i){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

在这里插入图片描述



五.函数重载

函数重载(overload);

函数名称一样 2.函数参数数据类型或者个数不同 3.跟返回值类型无关

面试题:

请简述overload(函数重载)、override(覆盖)、overwrite(重写)

思考:

函数重载所有编程语言都有的吗?

函数重载现象是强数据类型语言(java、C、C++、C#)所特有的现象

弱数据类型语言(javascript、python、ruby)是不具备函数重载



六.函数递归

函数递归(recursion):===循环 n>100用循环

函数自身调用自身

分治算法的理念

//重点内容:递归时的内存模型:

优点:分治算法的理念,复杂化简

缺点:非常耗费内存(栈溢出异常),当栈被全部占用后,触发StackOverflowError异常!!!

注意,可以使用java -X -Xss 大小, 来调整JVM的栈大小空间

-Xms<大小> 设置初始 Java 堆大小

-Xmx<大小> 设置最大 Java 堆大小



七.常见的系统类



1.Math

Math:

Math类,是JDK内置的类,Java.lang.Math

Java.lang:这个包,是默认导入的包



2.Random

随机数:

Math.random()

Random类

java.util.Random



3.String

String字符串对象:

在java中,被双引号,引主的内容就叫做字符串

字符串在java中不是基本数据,是对象,引用数据类型。

final修饰的,

字符串是常量,它们的值不能被创建后改变。支持可变字符串字符串缓冲区。

因为字符串对象是不可改变的,所以它们可以被共享。



字符串的各种方法

ToCharArray():将字符串转换成char类型的数组

new string(char[] chs):将一个字符数组转换成一个字符串

ToUpper():表示将一个字符串转换成大写形式。

ToLower():表示将一个字符串转换成小写形式。

Equals(“要比较的字符串”,StringComparison.OrdinalIgnoreCase):比较字符串,忽略大小写

Split(new char[]{‘要分割的字符串’},StringSplitOption.RemoveEmptyEntries):分割字符串,返回一个字符串类型的数组

Substring():截取字符串

Contains(“要判断的字符串”) 其值为布尔类型 判断是否包含某个字符子串

StartsWith(“要判断的字符串”) 其值为布尔类型 判断是否以某个字符子串开头

EndsWith(“要判断的字符串”) 其值为布尔类型 判断是否以某个字符子串结尾

int IndexOf(string value):取子串value第一次出现的位置

int LastIndexOf最后一次出现的位置



八.代码

//1.函数重载

class TestOverLoad {
	public static void main(String[] args) {
		byte a = 20;
		add(10, a);

		add(100);

		add(100, 20.56);

		add(20.56, 100);
	}

	public static int add(int a, int b) {
		System.out.println("----------1-------");
		return a + b;
	}

	public static double add(int a, double b) {
		System.out.println("----------1-------");
		return a + b;
	}

	public static double add(double a, int b) {
		System.out.println("----------1-------");
		return a + b;
	}

	public static double add(double a, double b) {
		System.out.println("----------1-------");
		return a + b;
	}

	public static int add(int a, byte b) {
		System.out.println("----------2-------");
		return a + b;
	}

	public static int add(int a) {
		System.out.println("----------3-------");
		return a + 10;
	}
}

//2.函数递归

class TestCecursion {
	public static void main(String[] args) {
		// System.out.println(getCount(100));

		System.out.println(getCount2(1000000));
	}
	/**递归也可,循环也可
	public static int getCount(int n) {
		int count = 0;
		for (int i = 1; i <= n; i++) {
			count += i;
		}
		return count;
	}
	**/
	// 递归  分治算法的理念
	public static int getCount2(int n) {
		
		if (n == 1) {
			return 1;
		}
		return n + getCount2(n - 1);
	}

//2.1

递归1:求第n项的斐波那契数列值

class TesFibonacii {
	public static void main(String[] args) {
		
		System.out.println(getFibo2(10));

	}

	// 递归方式求斐波那契数列的值?
	public static int getFibo(int n) {
		if (n == 1 || n == 2) {
			return 1;
		}
		return getFibo(n - 1) + getFibo(n - 2);
	}
  /**循环实现
	public static int getFibo2(int n) {
		int first = 1, second = 2;
		int temp;
		for (int i = 2; i <= n; i++) {
			temp = second;
			second = first + second;
			first = temp;
		}
		return first;
	}
**/

递归1变型:

上楼梯问题:某个人上楼梯,每次只能上一个台阶或者两个台阶,

那么当这个人到达第n个台阶时,共有多少种走法?

// 走楼梯问题
class TestStairs {
	public static void main(String[] args) {
		
		System.out.println(getStairs(10));

	}
	public static int getStairs(int n) {
		if (n == 0) {
			return 0;
		}
		if (n == 1) {
			return 1;
		}
		if (n == 2) {
			return 2;
		}
		return getStairs(n - 1) + getStairs(n - 2);
	}
}

递归2:

小明的妈妈看到小明今天高考结束,买了一对刚刚出生的小兔子,小兔子需要3个月长大成兔,之后每月生产一对小兔子,假如不考虑小兔子的死亡情况,求第n月共有多少对兔子?

1	1
2	1
3	1
4	2
5	3
6	4
7	6
8	9
9	13
public class TestRabbit {
	public static void main(String[] args) {
		System.out.println(getCount(14));

		Test02.getCount(10);
	}

	public static int getCount(int n) {
		if (n <= 4) {
			return 1;
		}
		return getCount(n - 1) + getCount(n - 4);
	}
}

//3.Math

class TestMath {
	public static void main(String[] args) {
		// 绝对值
		System.out.println(Math.abs(5));
		
		// ceil 向上取整
		System.out.println(Math.ceil(5.0001));
		// floor 向下取整
		System.out.println(Math.floor(5.9999));
		// 四舍五入
		System.out.println(Math.round(5.5));
		System.out.println(Math.round(5.4));
		
		// 最大值
		System.out.println(Math.max(10, 100));
		// 最小值
		System.out.println(Math.min(10, -100));

		// 幂次方
		System.out.println(Math.pow(2, 3));
		
		// random函数返回的了伪随机数:[0 , 1)
		System.out.println(Math.random()) ;



	}

	public static int abs(int n){
		return n > 0? n : -n;
	}

	public static double abs(double n){
		return n > 0? n : -n;
	}

	public static float abs(float n){
		return n > 0? n : -n;
	}

	public static long abs(long n){
		return n > 0? n : -n;
	}
}

//4.随机数

	public static void main(String[] args) {
		System.out.println(Math.random());
		// 0~10 的随机数
		System.out.println((int)(Math.random() * 10));

		// 15 ~ 40的 (m,n)随机数(n-m)+最小值m  (n>m)
		System.out.println((int)(Math.random() * 25) + 15);

		System.out.println(randomRange(10, 40));
	}

	public static int randomRange(int m, int n){
		if (m < n) {
			return (int)(Math.random()*(n - m)) + m;
		} else {
			return (int)(Math.random()*(m - n)) + n;
		}
	}
}
import java.util.Random;

class TestRandom02 {
	public static void main(String[] args) {
		Random r = new Random();

		System.out.println(r.nextBoolean());
		System.out.println(r.nextInt());

		System.out.println(r.nextInt(10));
	}
}

//5.String

class TestString {
	public static void main(String[] args) {
		String s = "liujianhong";
		String ss = "刘帅哥";
		String sss = "liujianhong";
		
		// 也可以通过如下方式,String的构造方法构造字符串
		String ssss = new String("liujianhong");

		System.out.println(s);
		System.out.println(ss);
		System.out.println(sss);
		System.out.println(ssss);

		// 就因为如此,所以,字符串不要使用== 判断是否相等
		System.out.println(s == sss);
		System.out.println(s == ss);
		System.out.println(s == ssss);
		
		// equals方法 来判断字符串是否相等
		System.out.println(s.equals(ssss));
		System.out.println(ssss.equals(sss));
	
	}
}

在这里插入图片描述



版权声明:本文为ly000666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。