package com.study.method;publicclassDemo01{//main方法publicstaticvoidmain(String[] args){// int sum = add(1, 2);// System.out.println(sum);test();}//加法 形参publicstaticintadd(int x,int y){int result =0;
result = x + y;return result;}publicstaticvoidtest(){for(int i =0; i <=1000; i++){if(i %5==0&& i !=0){
System.out.print(i +"\t");if(i %(5*3)==0){
System.out.print("\n");}}}}}
方法重载
package com.study.method;publicclassDemo02{publicstaticvoidmain(String[] args){// 方法的重载:名称相同,参数列表不同:个数,顺序,类型// int max = max(10, 20);double compare =compare(30.0,30.0);
System.out.println(compare);}//比大小publicstaticintcompare(int x,int y){int z = x > y ? x : y;return z;}publicstaticdoublecompare(double x,double y){double z = x > y ? x : y;return z;}publicstaticintmax(int x,int y){int max =-1;if(x > y){
max = x;}else{
max = y;}return max;}}
package com.study.method;publicclassDemo03{publicstaticvoidmain(String[] args){//args.length 数组长度for(int i =0; i < args.length; i++){
System.out.println("args["+i+"]"+args[i]);}}}
可变参数
package com.study.method;publicclassDemo04{publicstaticvoidmain(String[] args){
Demo04 demo04 =newDemo04();
demo04.test(10,20,30);}publicvoidtest(double x,int... i){// System.out.println(i[0]);for(int y : i
){
System.out.println(y);}}}
package com.study.method;publicclassDemo05{publicstaticvoidmain(String[] args){// printMax(10.0,20.0,4.0,6.0,0.0,50.0);printMax(newdouble[]{1,2,4,6,8});}publicstaticvoidprintMax(double... numbers){if(numbers.length ==0){
System.out.println("no argument passed");return;}double result = numbers[0];for(double x : numbers
){if(x > result){
result = x;}}
System.out.println("the max value is: "+ result);}}
递归
package com.study.method;publicclassDemo05{publicstaticvoidmain(String[] args){// printMax(10.0,20.0,4.0,6.0,0.0,50.0);printMax(newdouble[]{1,2,4,6,8});}publicstaticvoidprintMax(double... numbers){if(numbers.length ==0){
System.out.println("no argument passed");return;}double result = numbers[0];for(double x : numbers
){if(x > result){
result = x;}}
System.out.println("the max value is: "+ result);}}
数组
package com.study.arrays;publicclassArrayDemo01{publicstaticvoidmain(String[] args){int[] numbers;// 定义 首选int numbers2[]=newint[10];
numbers =newint[10];//这里面可以存放10个int类型的数字 创建一个数组// 给数组中的元素赋值
numbers[0]=1;for(int i =0; i < numbers.length; i++){
numbers[i]= i +1;
System.out.println(numbers[i]);}// 求和int sum =0;for(int x : numbers
){
sum += x;}
System.out.println(sum);}}
package com.study.arrays;publicclassArrayDemo03{publicstaticvoidmain(String[] args){int[] num ={1,2,3,4,5};//打印全部的数组元素for(int i =0; i < num.length; i++){
System.out.println(num[i]);}//计算所有的元素的和int sum =0;for(int i =0; i < num.length; i++){
sum += num[i];}
System.out.println(sum);
System.out.println("================");// 查找最大元素for(int i =1; i < num.length; i++){if(num[0]< num[i]){
num[0]= num[i];}}
System.out.println(num[0]);}}
package com.study.arrays;publicclassArrayDemo04{publicstaticvoidmain(String[] args){int[] arrays ={1,2,3,4,5};/*for (int array : arrays) {
System.out.println(array);
}*///print(arrays);reverse1(arrays);}// 打印数组元素 数组参数publicstaticvoidprint(int[] array){for(int i : array){
System.out.println(i);}}//翻转数组publicstaticint[]reverse(int[] arrays){int x =0;for(int i =0, j = arrays.length -1; i < j; i++, j--){
x = arrays[i];
arrays[i]= arrays[j];
arrays[j]= x;}for(int array : arrays){
System.out.println(array);}return arrays;}publicstaticint[]reverse1(int[] arrays){int[] result =newint[arrays.length];for(int i =0, j = arrays.length -1; i < arrays.length; i++, j--){
result[i]= arrays[j];}for(int i : result){
System.out.println(i);}return result;}}