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();
}
}
}