Java练习题11.2

Java练习题11.2


方法的定义和调用

1.编写一个方法,根据给出的长度和宽度打印矩形(*组成);

package com.shangjiti.aoian;
import java.util.Scanner;
public class No1 {
	public static void main(String[] args) {
		System.out.println("请输入两个整数:");
		Scanner sc=new Scanner(System.in);
		No1 test =new No1();
		int a=sc.nextInt();
		int b=sc.nextInt();
		test.method(a,b);
	}
	public int method(int m,int n) {
		for(int i=0;i<n;i++) 
		{
			for(int j=0;j<m;j++)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
		return n*m;
	}
}

2.编写一个方法,用来打印99乘法表;

package com.shangjiti.aoian;
public class No2 {
	public static void main(String[] args) {	 
		nineSort();	      
	}
public static void nineSort(){
    for (int i = 1; i <= 9; i++) 
    {
      for (int j = 1; j <= i; j++) 
      {
        System.out.print(j+"*"+i+"="+j*i+"\t");
      }
      System.out.println();
    }
  }
}

3.编写一个方法,用来打印99乘法表的前n行;

package com.shangjiti.aoian;
import java.util.Scanner;
public class No3 {
	public static void main(String[] args) {
		System.out.println("请输入一个整数:");
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		nineSort(n);
	}
	public static void nineSort(int n){
	for (int i = 1; i <= n; i++) 
    {
      for (int j = 1; j <= i; j++) 
      {
        System.out.print(j+"*"+i+"="+j*i+"\t");
      }
      System.out.println();
    }
  }
}

4.编写一个方法,接收一个整数数组作为参数,此方法会找出其最大值或最小值;

package com.shangjiti.aoian;
public class No4 {
	public static void main(String[] args) {
		int[] a = {4,66,99,8,63,26,45,36,42,88,11,555,64,23,75};
		number(a);
	}
	public static void number(int[] b){
		int i_max = 0;
		int i_min = 0;
		for(int i = 1; i < b.length; i++){
			if(b[i_max] < b[i])
				i_max = i;
			if(b[i_min] > b[i])
				i_min = i;
		}
		System.out.println("最大值: "+b[i_max]);
		System.out.println("最小值: "+b[i_min]);
	}
}

5.编写一个方法,根据给出的行数n,输出杨辉三角形的前n行;

package com.shangjiti.aoian;
import java.util.Scanner;
public class No5 {
	public static void main(String[] args) {
		System.out.println("请输入行数: ");
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		yanghui(n);
	}
	public static void yanghui(int n){
		int[][] a = new int[n][n];
		int i;
		int j;
		for(i = 0; i < n; i++){
			for(j = 0; j <= i; j++){
				if( i == j || j == 0)
					a[i][j] = 1;
				else
					a[i][j] =a[i-1][j-1] + a[i-1][j];
				System.out.print(a[i][j]+"\t");
			}	
			System.out.println();
		}
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值