JAVA学习——函数总结

本文详细介绍了Java中的函数,包括函数的定义、调用方式、分类,以及函数重载和递归的概念。讲解了Math、Random和String等常见系统类的使用,并提供了代码示例,如斐波那契数列、上楼梯问题和兔子繁殖问题的递归解决方案。此外,还讨论了函数重载在不同编程语言中的特性。
摘要由CSDN通过智能技术生成

记住格式

                //拿来一个扫描器:
                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)
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值