Java SE 第五章

第5章 函数
5.1 方法定义
※ 修饰符
在这里插入图片描述
在这里插入图片描述
※ 返回值类型
返回值类型是需要什么就写什么。
※ 函数名

※ 参数列表

※ 函数体
修饰符返回值类型函数名(形式参数类型1 参数名1,形式参数类型2 参数名2,形式参数类型3 参数名3…)

{

函数体语句;

return 返回值;

}
5.2 方法调用
※ 函数调用函数

※ 函数递归调用

5.3 实际参数和形式参数
※ 实际参数
是在执行时,调用函数或过程时,传递给函数或过程的参数。通俗讲就是实际值。
※ 形式参数
就是在定义函数或过程的时候命名的参数。通俗讲就是一个记号。形参就是定义函数时候的参数表,只是定义了参数表的结构和用来引用的名字,并没有具体的内容。
实参是调用函数传递的具体数据。
※ 参数传值

5.4 方法重载
※ 方法重载的定义
如果有两个方法的方法名相同,但参数不一致,那么可以说一个方法是另一个方法的重载。
※ 方法重载的条件
方法名相同
方法的参数类型,参数个不一样
方法的返回类型可以不相同
方法的修饰符可以不相同
main 方法也可以被重载
※ 方法重载的注意项
参数列表必须相同
重载和参数变量名无关
重载和返回值类型无关
重载和修饰符无关
5.5 变量的作用域
※ 局部变量
“在函数内定义的变量”,

即在一个函数内部定义的变量,只在本函数范围内有效。
※ 全局变量
“在函数外定义的变量”,

即从定义变量的位置到本源文件结束都有效。  
5.6 常用数学函数
※ 三角函数
在这里插入图片描述
※ 指数函数
在这里插入图片描述
※ 取整函数
在这里插入图片描述
※ min、max和abs方法
Math.min(n1,n2) n1和n2之间的最小值

Math.max(n1,n2) n1和n2之间的最大值

Math.abs(n) 求n的绝对值
※ random方法

Math.random() 产生一个[0,1)的随机数
※ Random类
Random():创建一个新的随机数生成器。

Random(long seed):使用单个 long 种子创建一个新的随机数生成器。

第一种构造方法是使用默认当前系统时间的毫秒数作为种子数:Random r1 = new Random();

Random random = new Random();
int randomNumber1= random.nextInt(100);

第二种方法是使用自己指定的种子数

Random random1 = new Random(100);
for(int i = 0; i < 10; i++){
System.out.print(random1.nextInt(10) + " ");
}

发现只要种子数和nextInt()中的参数一致的话,每次生成的随机数都是一样的(所以这是伪随机数)。

System.out.println("\n使用同一种子生成的随机数如下:");
Random random2 = new Random(100);
for(int i = 0; i < 10; i++){
System.out.print(random2.nextInt(10) + " ");
}
※ 示例:计算三角形的角度
在这里插入图片描述

import java.util.Scanner;


public class Test1 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入三个点的坐标
		System.out.print("Enter three points:");
		double x1=scanner.nextDouble();
		double y1=scanner.nextDouble();
		double x2=scanner.nextDouble();
		double y2=scanner.nextDouble();
		double x3=scanner.nextDouble();
		double y3=scanner.nextDouble();
		//2.根据坐标计算三条边
		double a=getDistance(x2, y2, x3, y3);
		double b=getDistance(x1, y1, x3, y3);
		double c=getDistance(x1, y1, x2, y2);
		//3.根据三边计算三个角的角度
		double A=getDegree(a,b,c);
		double B=getDegree(b, a, c);
		double C=getDegree(c, a, b);
		System.out.println("The three angles are "+A+","+B+","+C);
	}
	public static double getDegree(double acrossSide,double neighberSide1,double neighberSide2){
		//弧度值
		double degree=Math.acos( (Math.pow(acrossSide, 2)-Math.pow(neighberSide1, 2)-Math.pow(neighberSide2, 2))/(-2*neighberSide1*neighberSide2) );
		//将弧度值转换为角度值 并返回
		return Math.toDegrees(degree);
	}
	public static double getDistance(double x1,double y1,double x2,double y2){
		double distance=Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
		return distance;
	}
}

5.7 String相关函数
※ 求字符串长度
※ 从字符串中获取字符
※ 连接字符串
※ 字符串的转换
※ 从控制台读取字符串
※ 从控制台读取字符
在这里插入图片描述
※ 字符串比较
在这里插入图片描述
※ 获取字符串中的字符或子串
在这里插入图片描述
※ 字符串和数字间的转换

※ 示例:将十六进制数转换为十进制数
在这里插入图片描述

import java.util.Scanner;


public class Test5_2 {
	public static void main(String[] args) {
		System.out.print("Enter a hex number :");
		Scanner scanner=new Scanner(System.in);
		String str=scanner.nextLine();
		double number=hexToDecimal(str);
		System.out.println(number);
	}
	public static double hexToDecimal(String hex){
		hex=hex.trim();
		hex=hex.toLowerCase();
		double sum=0;
		int mi=0;
		for(int i=hex.length()-1;i>=0;i--){
			char c=hex.charAt(i);
			if(isDigit(c)){
				sum=sum+(c-48)*Math.pow(16, mi);
				mi++;
			}else if(isLetter(c)){
				sum=sum+(c-87)*Math.pow(16, mi);
				mi++;
			}else{
				throw new IllegalArgumentException("非法字符!"+c);
			}
		}
		return sum;
	}
	public static boolean isDigit(char c){
		if(c>=48&&c<=57){
			return true;
		}else{
			return false;
		}
	}
	public static boolean isLetter(char c){
		if(c>=97&&c<=102){
			return true;
		}else{
			return false;
		}
	}
}

本章小结
1.程序模块化和可重用性是软件工程的中心目标之一。 Java 提供了很多有助于完成这一目标的有效结 构。 方法就是一个这样的结构。

2.方法头指定方法的修饰符、 返回值类型、 方法名和参数。 本章所有的方法都使用静态修饰符static

3 .方法可以返回一个值。 返回值类型 returnValueType 是方法要返回的值的数据类型。 如果方法不 返回值, 则返回值类型就是关键字 void。

4.参数列表是指方法中参数的类型、 次序和数置。 方法名和参数列表一起构成方法签名( method signature)。 参数是可选的, 也就是说, 一个方法可以不包含参数。

5.return 语句也可以用在 void 方法中, 用来终止方法并返回到方法的调用者。 在方法中, 有时用于 改变正常流程控制是很有用的。

6.传递给方法的实际参数应该与方法签名中的形式参数具有相同的数目、 类型和顺序。

7.当程序调用一个方法时, 程序控制就转移到被调用的方法。 被调用的方法执行到该方法的 return 语句或到达方法结束的右括号时, 将程序控制还给调用者。

8.在 Java 中, 带返回值的方法也可以当作语句调用。 在这种情况下, 调用函数只要忽略返回值即可。

9.方法可以重载。 这就意味着两个方法可以拥有相同的方法名, 只要它们的方法参数列表不同即可。

10.在方法中声明的变量称作局部变量。 局部变量的作用域是从声明它的地方开始. 到包含这个变釐的 块结束为止。 局部变量在使用前必须声明和初始化。

11.方法抽象是把方法的应用和实现分离。 用户可以在不知道方法是如何实现的情况下使用方法。 方法 的实现细节封装在方法内, 对调用该方法的用户隐藏。 这称为信息隱藏或封装。

12.方法抽象将程序模块化为整齐、 层次分明的形式。 将程序写成简洁的方法构成的集合会比其他方式 更容易编写、 调试、 维护和修改。 这种编写风格也会提高方法的可重用性。

13.当实现一个大型程序时, 可以使用自顶向下或自底向上的编码方法。 不要一次性编写完整个程序。 这种方式似乎浪费了更多的编码时间(因为要反复编译和运行这个程序), 但实际上, 它会更节省 时间并使调试更容易。

编程练习题
在这里插入图片描述

import java.util.Scanner;


public class Demo5_1 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a number:");
		long number=scanner.nextLong();
		int sum=sumDigits(number);
		System.out.println(sum);
	}
	//1212
	public static int sumDigits(long n){
		int sum=0;
		while(true){
			sum+=n%10;
			n=n/10;
			if(n==0){
				break;
			}
		}
		return sum;
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_2 {
	public static void main(String[] args) {
		//123 321 121 121
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a number:");
		int number=scanner.nextInt();
		System.out.println(isPailindrome(number));
	}
	public static int reverse(int number){
//		String s="";
//		while(true){
//			s+=number%10;
//			number/=10;
//			if(number==0){
//				break;
//			}
//		}
		String numberStr=""+number;//"12321"
		String s="";
		for(int i=numberStr.length()-1;i>=0;i--){
			s+=numberStr.charAt(i);
		}
		return Integer.parseInt(s);
	}
	public static boolean isPailindrome(int number){
		return reverse(number)==number;
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_3 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("请输入一个整数:");
		int number=scanner.nextInt();
		reverse(number);
	}
	public static void reverse(int number){
		String s="";
		int n=0;
		while(true){
			n=number%10;
			s+=n;
			number/=10;
			if(number==0){
				break;
			}
		}
		System.out.println("它的反向数是:"+s);
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_4 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter three numbers:");
		double num1=scanner.nextDouble();
		double num2=scanner.nextDouble();
		double num3=scanner.nextDouble();
		//最终 num1存最小值 num2存中间值 num3存最大值
		displaySortedNumbers(num1, num2, num3);
	}
	public static void displaySortedNumbers(double num1,double num2,double num3){
		
		double temp=0.0;
		if(num1>num2&&num1>num3){//num1最大
			temp=num3;
			num3=num1;
			num1=temp;
			if(num1>num2){
				temp=num2;
				num2=num1;
				num1=temp;
			}
		}else if(num2>num1&&num2>num3){//num2最大
			temp=num3;
			num3=num2;
			num2=temp;
			if(num1>num2){
				temp=num2;
				num2=num1;
				num1=temp;
			}
		}else if(num3>num1&&num3>num2){//num3最大
			if(num1>num2){
				temp=num2;
				num2=num1;
				num1=temp;
			}
		}
		System.out.println(num1+"<"+num2+"<"+num3);
		
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_5 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter the line:");
		int line=scanner.nextInt();
		displayPattern(line);
	}
	public static void displayPattern(int line){
		for(int i=1;i<=line;i++){
			for(int k=1;k<=line-i;k++){
				System.out.print("  ");
			}
			for(int j=i;j>=1;j--){
				System.out.print(j+" ");
			}
			System.out.println();
		}
	}
}

在这里插入图片描述


public class Demo5_6 {
	public static void main(String[] args) {
		for(int celsius=-10;celsius<=40;celsius++){
			System.out.println(celsius+":"+celsiusToFahrenheit(celsius));
		}
	}
	public static double celsiusToFahrenheit(double celsius){
		return (9.0/5)*celsius+32;
	}
	public static double fahrenheitToCelsius(double fahrenheit){
		return (5.0/9)*(fahrenheit-32);
	}
}

在这里插入图片描述


public class Demo5_7 {
	public static void main(String[] args) {
		for(double foot=1.0;foot<=10.0;foot++){
			System.out.println(foot+":"+footToMeter(foot));
		}
	}
	public static double footToMeter(double foot){
		return 0.305*foot;
	}
	public static double meterToFoot(double meter){
		return 3.279*meter;
	}
}

在这里插入图片描述


public class Demo5_8 {
	public static void main(String[] args) {
		for(int i=1;i<=20;i++){
			System.out.println(i+":"+m(i));
		}
	}
	public static double m(int i){
		double sum=0.0;
		for(int n=1;n<=i;n++){
			
			sum+=1.0*n/(n+1);
		}
		return sum;
	}
}

在这里插入图片描述


public class Demo5_9 {
	public static void main(String[] args) {
		for(int i=1;i<=901;i+=100){
			System.out.println(i+":"+m(i));
		}
	}
	public static double m(int i){
		int flag=1;
		double sum=0.0;
		for(int n=1;n<=i;n++){
			sum+=1.0*flag/(2*n-1);
			flag=-flag;
		}
		
		return 4*sum;
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_10 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("请输入n的值:");
		int n=scanner.nextInt();
		printMatrix(n);
	}
	public static void printMatrix(int n){
		
		int count=0;
		for(int i=1;i<=n;i++){
			for(int k=1;k<=n;k++){
				System.out.print((int)(Math.random()*3));
				count++;
			}
			if(count%n==0){
				System.out.println();
			}
		}
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_11 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a password:");
		String password=scanner.nextLine();
		if(isPasswordValid(password)){
			System.out.println("Valid Password");
		}else{
			System.out.println("Invalid Password");
		}
		//return
	}
	public static boolean isPasswordValid(String password){
		boolean condition1=isLengthMoreThanEight(password);
		boolean condition2=isOnlyContainsLetterAndDigit(password);
		boolean condition3=isContainsMoreThanTwoDigits(password);
		if(condition1&&condition2&&condition3){
			return true;
		}else{
			return false;
		}
	}
	public static boolean isLengthMoreThanEight(String password){
		return password.length()>=8;
	}
	public static boolean isOnlyContainsLetterAndDigit(String password){
		for(int i=0;i<password.length();i++){
			char c=password.charAt(i);
			if(!(isLetter(c)||isDigit(c))){
				return false;
			}
//			if( isNotLette&&isNotDigit ){
//				return false;
//			}
		}
		return true;
	}
	private static boolean isDigit(char c) {
		return c>='0'&&c<='9';
	}
	private static boolean isLetter(char c) {
		return c>='a'&&c<='z'||c>='A'&&c<='Z';
	}
	public static boolean isContainsMoreThanTwoDigits(String password){
		return getDigitCount(password)>=2;
	}
	private static int getDigitCount(String password) {
		int count=0;
		for(int i=0;i<password.length();i++){
			char c=password.charAt(i);
			if(isDigit(c)){
				count++;
			}
		}
		return count;
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_12 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("请分别输入三边长度:");
		double side1=scanner.nextDouble();
		double side2=scanner.nextDouble();
		double side3=scanner.nextDouble();
		if(isValid(side1, side2, side3)){
			System.out.println("三角形面积为"+":"+area(side1, side2, side3));
		}else{
			System.out.println("三边输入非法!");
		}
		
	}
	public static boolean isValid(double side1,double side2,double side3){
		if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1){
			return true;
		}else{
			return false;
		}
	}
	public static double area(double side1,double side2,double side3){
		double s=(side1+side2+side3)/2;
		double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
		return area;
	}
}

在这里插入图片描述

import java.util.Scanner;


public class Demo5_13 {
	public static void main(String[] args) {
		System.out.print("Enter a number:");
		System.out.println(sqrt(new Scanner(System.in).nextLong()));
	}
	public static double sqrt(long n){
		double nextGuess=0.0;
		double lastGuess=1.0;
		while(true){
			nextGuess=(lastGuess+n/lastGuess)/2;
			if(Math.abs(nextGuess-lastGuess)<0.0001){
				break;
			}else{
				lastGuess=nextGuess;
			}
		}
		return nextGuess;
	}
}

在这里插入图片描述


public class Demo5_14 {
	public static void main(String[] args) {
		System.out.println(count("ashdkgasdhjhsagdiasd", 'a'));
	}
	public static int count(String str,char a){
		int count=0;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)==a){
				count++;
			}
		}
		return count;
				
	}
}

在这里插入图片描述



public class Demo5_16 {
	public static void main(String[] args) {
		int count=0;
		int num=2;
		while(true){
			if(isSuShu(num)&&isHuiWen(num)){
				System.out.printf(num+" ");
				count++;
				if(count%10==0){
					System.out.println();
				}
				if(count==100){
					break;
				}
			}
			num++;
		}
	}
	public static boolean isSuShu(int num){
		boolean flag=true;
		for(int i=2;i<=num/2;i++){
			if(num%i==0){
				flag=false;
				break;
			}
		}
		return flag;
	}
	public static boolean isHuiWen(int num){
		String s="";
		int temp=num;
		while(true){
			s+=temp%10;
			temp/=10;
			if(temp==0){
				break;
			}
		}
		return Integer.parseInt(s)==num;
	}
}

猜你喜欢

转载自blog.csdn.net/w_Antetokounmpo/article/details/83215294
今日推荐