Java函数编程练习

【练习题】

编写两个函数,分别求最大公约数(greatest common divisor)和最小公倍数(least common multiple)。

解:

(1)函数原型与参数说明:

最大公约数函数:int gcd(int a,int b)

形参与函数类型

含义

int a

第一个数

int b

第二个数

int gcd()

返回最大公约数

最小公倍数函数:int lcm(int a,int b,int g)

形参与函数类型

含义

int a

第一个数

int b

第二个数

int g

两个数的最大公约数

int gcd()

返回最小公倍数

public class Test1 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("请输入两个正整数:");
		int n = reader.nextInt();
		int m = reader.nextInt();

		System.out.println("这个数的最大公约数是:" + gcd(m, n));
		System.out.println("这个数的最小公倍数是:" + lcm(m, n));

	}

	static int gcd(int a, int b) {
		int i;
		for (i = (b < a ? b : a); i >= 0; i--)
			if (a % i == 0 && b % i == 0)
				break;
		return i;
	}

	static int lcm(int a, int b) {

		return a * b / gcd(a, b);
	}

}

 

【练习题】

编写一个判断素数(prime number)的函数。

解:

(1)函数原型与参数说明:

求素数函数:int prime (int n)

形参与函数类型

含义

int n

一个整数

int prime ()

是素数返回1,不是素数返回0

public class Test2 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("请输入一个正整数:");
		int m = reader.nextInt();
		if (prime(m))
			System.out.println(m + "是素数");
		else
			System.out.println(m + "不是素数");
	}

	static boolean prime(int m) {
		if (m == 1)
			return false;
		for (int i = 2; i < m; i++)
			if (m % i == 0)
				return false;
		return true;
	}

}

 

【练习题】

编写函数,使得给定的一个二维数组(3*3)转置(transposition)。

解:

(1)函数原型与参数说明:

二维数组转置函数:int [][] trans (int a[][])

形参与函数类型

含义

int a[][]

数组名

Int[][] trans ()

返回值,通过传址实现转置

public class Test3 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("输入:");
		int a[][] = new int[3][3];

		for (int i = 0; i < a.length; i++)
			for (int j = 0; j < a[i].length; j++)
				a[i][j] = reader.nextInt();

		int b[][] = trans(a);
		printArray(b);
	}

	static void printArray(int b[][]) {
		if (b == null)
			return;
		for (int i = 0; i < b.length; i++)
			System.out.println(Arrays.toString(b[i]));
	}

	static int[][] trans(int a[][]) {
		if (a == null)
			return null;
		int b[][] = new int[a.length][a.length];
		int i, j;

		for (i = 0; i < a.length; i++)
			for (j = 0; j < a[i].length; j++)
				b[j][i] = a[i][j];

		return b;

	}

}

 

【练习题】

编写一个函数,使得输入的一个字符串反序存放。

解:

(1)函数原型与参数说明:

反序函数:void inverse (char c[])

形参与函数类型

含义

char c[]

字符数组名

void inverse ()

没有返回值,通过传址实现反序

public class Test4 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("输入:");
		String s = reader.nextLine();

		char ch[] = s.toCharArray();
		inverse(ch);
		System.out.println(ch);
	}
	static void inverse (char c[]){
		for (int i=0; i<c.length/2; i++){
	        char t=c[i];
	        c[i]=c[c.length-1-i];
	        c[c.length-1-i]=t;
	    }
			
	}	

}

 

【练习题】

编写一个函数,连接(connect)两个字符串。

解:

(1)函数原型与参数说明:

字符串连接函数:char[] connect (char s1[],char s2[])

形参与函数类型

含义

char s1 []

字符串1

char s2 []

字符串2

Char[] connect ()

返回值,通过传址实现字符串连接

public class Test5 {
	/*
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("输入:");
		String s1 = reader.nextLine();
		String s2 = reader.nextLine();
		
		char c1[] = s1.toCharArray();
		char c2[] = s2.toCharArray();
		System.out.print(connect(c1,c2));
	}
	
	static char[] connect (char s1[],char s2[]){
		char s[]=new char[s1.length+s2.length];		
		for(int i=0;i<s1.length;i++){
			s[i]=s1[i];
		}
		for(int j=0;j<s2.length;j++){
			s[s1.length+j]=s2[j];
		}
		return s;		
	}
	*/
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("输入:");
		String s1 = reader.nextLine();
		String s2 = reader.nextLine();
				
		System.out.print(connect(s1,s2));
	}
	
	static String connect (String s1,String s2){
		
		return s1+s2;		
	}
}

 

【练习题】

编写一个函数,将一个字符串中的元音字母(vowel:a、e、i、o、u)复制到另一个字符串中。

解:

(1)函数原型与参数说明:

元音字母复制函数:void vowel (char s1[],char s2[])

形参与函数类型

含义

char s1 []

字符串

char s2 []

元音字母字符串

void vowel ()

没有返回值,通过传址实现元音字母复制

public class Test6 {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("输入:");
		String s = reader.nextLine();

		char s1[] = s.toCharArray();
		char s2[]=new char[s1.length];
		vowel(s1,s2);
		System.out.println(s2);
		
		int count = 0;
		for(int i=0;i<s2.length;i++){
			if(s2[i]=='a'||s2[i]=='e'||s2[i]=='i'||s2[i]=='o'||s2[i]=='u')
				count++;
			continue;
		}
		for(int i=0;i<count;i++)
			System.out.print(s2[i]);			    	   
	}
	
	static void vowel (char s1[],char s2[]){	
		int j = 0;
		for(int i=0;i<s1.length;i++)
			if(s1[i]=='a'||s1[i]=='e'||s1[i]=='i'||s1[i]=='o'||s1[i]=='u')	
				s2[j++]=s1[i];	
	}

}

 

【练习题】

编写一个函数,将字符串中最长的单词输出。

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值