练习:(使用无参或者有参无返回值的方法进行) 1.需求:打印一个5行的直角三角形 2.需求:打印一个8行的直角三角形 3.需求:打印一个10行的直角三角形 练习:(使用有参无返回值的方法实现) 1.需求:帮我们遍历int型数组 2.帮助我们对int数组,进行 小-大排序 练习:(使用无参有返回值的方法实现) 1.需求:随机生成1个四位数字返回 2.需求:随机生成3个四位数字返回 3.需求:随机生成5个四位数字返回 练习: 需求:帮组我们在char数组中找指定的字符,返回脚标,如果找不到则返回-1
test1
public class Test1 {
public static void main(String[] args) {
MyUitls myUitls = new MyUitls();
myUitls.printN(10);
}
}
test2
public class Test2 {
public static void main(String[] args) {
int[] abc={1,2,3,45,23,12,33,7};
//想看数组中的数据(自己写循环)
MyUitls myUitls = new MyUitls();
myUitls.showIntArray(abc);
System.out.println("-----------");
myUitls.sort(abc);//对abc进行排序
myUitls.showIntArray(abc);
}
}
test3
public class Test3 {
public static void main(String[] args) {
MyUitls myUtil = new MyUitls();
int i = myUtil.random1Num();
System.out.println("---"+i);
int[] ints = myUtil.random5Num();
myUtil.showIntArray(ints); //查看int[]
}
}
调用的类如下集合
public class MyUitls {
public void fun(int[] a,int b,String c){
//a,b,c称之为形参
//在定义fun方法的时候,abc的值是不清楚的,形式参数(当做有值去使用)
}
/*
在指定的数组中,找字符
参数列表:char[] , char
返回值 int
需要输出语句? 不需要输出语句
结果的输出,还是做其他用途,是调用者说了算,和本方法无关
参数(arrs)的长度也没声明呀,arrs的长度以及内部的数据,都是由调用者决定
如果有两个一样的,我们这个程序返回第一个的脚标!(扩展:返回所有脚标)
返回值类型和参数类型,并没有任何关系
* */
public int findCharByChars(char[] arrs,char c){
for (int i = 0; i < arrs.length; i++) {
if (arrs[i] == c)
return i;
}
return -1;
}
public int random1Num(){
int num = (int)(Math.random()*9000+1000);
return num;
}
public int[] random3Num(){
int[] arrs=new int[3];
for (int i = 0; i < arrs.length; i++) {
int num = (int)(Math.random()*9000+1000);
arrs[i]=num;
}
return arrs;
}
public int[] random5Num(){
int[] arrs=new int[5];
for (int i = 0; i < arrs.length; i++) {
int num = (int)(Math.random()*9000+1000);
arrs[i]=num;
}
return arrs;
}
/*
对任意int型数组,进行小-大排序
* */
public void sort(int[] arrs){
//冒泡排序
for (int i = 0; i < arrs.length-1; i++) {
for (int j = 0; j < arrs.length-i-1; j++) {
if(arrs[j]>arrs[j+1]){
int temp= arrs[j];
arrs[j]=arrs[j+1];
arrs[j+1]=temp;
}
}
}
}
/*
功能:遍历int[]
* */
public void showIntArray(int[] arrs){
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}
}
/*
打印n行直角三角形
* */
public void printN(int num){
for (int i = 0; i < num; i++) {
for (int j = 0; j < i+1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}