首页 > 代码库 > Java入门——数组和方法
Java入门——数组和方法
练习题
求100——1000内的水仙花数
1 public class Shuixianhua { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 int count=1; 9 for(int i=100;i<=1000;i++){10 int a=(i-i%100)/100;11 int b=(i-100*a)/10;12 int c=i%10;13 int testNum=a*a*a+b*b*b+c*c*c;14 if((testNum==i)&(0<a)&(a<10)){15 System.out.println("第"+count+"个水仙花数是"+i);16 count++;17 }18 }19 }
第1个水仙花数是153第2个水仙花数是370第3个水仙花数是371第4个水仙花数是407
求三个数的最大值
1 public class Max { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 int a=12; 9 int b=32;10 int c=321;11 if(a<b){12 a=b;13 if(a<c){14 a=c;15 }16 }else{17 if(a<c)18 a=c;19 }20 System.out.println("最大值是"+a);21 }23 }
判断某数是否能同时被3、5、7整除
1 //判断某数是否能同时被3、5、7整除 2 public class IfDemo2 { 3 4 /** 5 * @param args 6 */ 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 for(int i=1;i<1000;i++){10 if (i%3==0){11 if(i%5==0){12 if(i%7==0){13 System.out.println(i);14 }15 }16 }17 }18 }19 20 }
数组
1 package Sep10; 2 3 public class ArrayDemo01 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int [] score=null;11 score=new int[3];12 System.out.println("score[0]="+score[0]);13 System.out.println("score[1]="+score[1]);14 System.out.println("score[2]="+score[2]);15 System.out.println("******************");16 for(int i=0;i<3;i++){17 System.out.println("score["+i+"]="+score[i]);18 }19 20 }21 22 }
score[0]=0score[1]=0score[2]=0******************score[0]=0score[1]=0score[2]=0
静态初始化:用{--------}
1 package Sep10; 2 3 public class ArrayDemo04 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int score[]={0,1,3,5,23};11 for(int x=0;x<score.length;x++){12 System.out.println("score["+x+"]:"+score[x]);13 }14 }15 16 }
score[0]:0score[1]:1score[2]:3score[3]:5score[4]:23
求数组中的最大值和最小值:
package Sep10;//求数组中的最大值和最小值public class ArrayDemo05 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int score[]={12,42,432,43,54,65}; int max=score[0]; int min=score[0]; for(int i=0;i<score.length;i++){ if(score[i]>max){ max=score[i]; } if(score[i]<min){ min=score[i]; } } System.out.println("最大值是:"+max); System.out.println("最小值是:"+min); }}
最大值是:432最小值是:12
冒泡法排列
1 package Sep10; 2 //数组元素排序 3 public class ArrayDemo06 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int score[]={4232,123,312,423,43,321};11 for(int i=0;i<score.length;i++){12 for(int j=i+1;j<score.length;j++){13 if(score[j]<score[i]){14 int middle=score[i];15 score[i]=score[j];16 score[j]=middle;17 }18 }19 }20 for(int i=0;i<score.length;i++){21 System.out.println("排列为:"+score[i]);22 }23 }24 25 }
排列为:43排列为:123排列为:312排列为:321排列为:423排列为:4232
二维数组
动态初始化
1 package Sep10; 2 //动态初始化数组 3 public class ArrayDemo07 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int score[][]=new int[3][4];11 score[0][1]=01;12 score[1][2]=12;13 score[0][2]=02;14 score[2][1]=21;15 score[2][0]=20;16 for(int i=0;i<score.length;i++){17 for(int j=0;j<score.length;j++){18 System.out.print(score[i][j]+"\t");19 }20 System.out.println("");21 }22 }23 24 }
0 1 2 0 0 12 20 21 0
方法
- 1:没有形参直接写();2:没有返回值注明void,可以省略return语句;
1 package Sep10; 2 //调用加减法函数 3 public class Method01 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int test1=addInt(12,32);11 float test2=addFloat(12.2f,32f);12 System.out.println(test1);13 System.out.println(test2);14 }15 public static int addInt(int x,int y){16 int sum=x+y;17 return sum;18 }19 public static float addFloat(float x,float y){20 float sum=x+y;21 return sum;22 }23 }
4444.2
1 package Sep10; 2 //方法的重载 3 public class Method02 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int test1=add(12,32);11 float test2=add(12.2f,32f);12 System.out.println(test1);13 System.out.println(test2);14 }15 public static int add(int x,int y){16 int sum=x+y;17 return sum;18 }19 public static float add(float x,float y){20 float sum=x+y;21 return sum;22 }23 24 }
Java中可以使用return直接停止方法的使用
向方法中传递数组
1 package Sep10; 2 //向方法中传递数组 3 public class Method03 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 int[] temp={0,2,1};11 fun(temp);//不用加[]12 System.out.println(temp[0]);13 }14 public static void fun(int x[]){15 x[0]=6;16 }17 }
6
Java入门——数组和方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。