Java入门——数组和方法

  • 练习题

  1. 求100——1000内的水仙花数

  2.  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. 第1个水仙花数是153
    第2个水仙花数是370
    第3个水仙花数是371
    第4个水仙花数是407
  2. 求三个数的最大值

  3.  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 } 
  4. 判断某数是否能同时被3、5、7整除

  5.  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 stub
    10         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]=0
    score[1]=0
    score[2]=0
    ******************
    score[0]=0
    score[1]=0
    score[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 stub
    10         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]:0
    score[1]:1
    score[2]:3
    score[3]:5
    score[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 stub
    10         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 stub
    10         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 stub
    10         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 }
    44
    44.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 stub
    10         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 stub
    10         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
时间: 2024-08-09 22:37:28

Java入门——数组和方法的相关文章

JAVA入门学习: 方法参数的传递(函数传参问题)

引言:我们知道C++中拥有两种参数传递方式, 值调用和引用调用. 有些程序员认为JAVA程序设计语言对对象采用的是引用调用,实际上,这种理解是不对的. 由于这种误解存在普遍性,所以下面将阐述一下这个问题. 所以用一段简单的代码来阐述一下这个问题: 1 //如果方法参数是的输入时引用,那么我们将交换两个指针(即引用) 2 public static void swap(Employee x, Employee y) 3 { 4 Employee temp = x; 5 x =y; 6 y = te

Java翻转数组的方法

java的api没用翻转数组的工具类,只能自己写了. int [] testIntArr = {1,2,3}; //翻转数组 for (int i = 0; i < testIntArr.length / 2; i++) { Object temp = testIntArr[i]; testIntArr[i] = testIntArr[testIntArr.length - 1 - i]; testIntArr[testIntArr.length - 1 - i] = (int) temp; }

java入门学习:Java中的main()方法详解

本文来源:http://www.zretc.com/technologyDetail/445.html 在Java入门学习中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是public static void 类型的,方法必须接收一个字符串数组的参数等等. 在看Java中的main()方法之前,先看一个最简单的Java应用程序HelloWorld,我将通过这

JAVA入门之数组

什么是数组? 数组可以理解为是一个巨大的“盒子”,里面可以按顺序存放多个类型相同的数据,比如可以定义 int 型的数组 scores 存储 4 名学生的成绩 数组中的元素都可以通过下标来访问,下标从 0 开始.例如,可以通过 scores[0] 获取数组中的第一个元素 76 ,scores[2] 就可以取到第三个元素 92 啦! Java 操作数组四个步骤: 1. 声明数组 语法:  数据类型[ ] 数组名: 或者   数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名,如: 2. 分

[Java入门笔记] Java语言基础(五):数组

简介 数组可用用于存储存储多个数据,Java的数组要求所有的数组元素具有一种相同的数据类型.一旦数组初始化完成,数组在内存中的空间被固定下来,长度不可改变,即使把数组的元素清空,所占用的空间依然被保留. 生活案例:博物架 每一个物品架都是相同类型的物品,长度不变,即使把物品下架,物品架依然不会改变. 定义数组 使用数组4步走: 1.声明数组 Java中支持两种格式的数组定义格式: 类型[] 变量名; 类型 变量名[]; 例: int[] a; int b[]; //两种方法都行,不过建议使用第一

将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed income", "futures", "options"}; List<String> assetList = Arrays.asList(asset);

去掉有序数组中重复数字 原地 leetcode java (最简单的方法)

1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

JAVA遍历二位数组的方法

//使用方法对于二维数组进行遍历 package com; import java.util.Arrays; public class CompoundInterest { public static void main(String[] args) { // TODO Auto-generated method stub double[][] balances= new double[3][3]; for(int j = 0; j < balances[0].length; j++){ bal

Java中的数组和方法

3.1 数组的定义和使用 数组(Array)是用来存储一组相同数据类型数据的集合.数组中的每个数据称为一个元素(element),数组可以分为一维数组,二维数组和多维数组.我们 主要讲解一维数组和二维数组. 3.1.1一维数组的声明数组变量 Java中的数组必须先声明然后再使用,Java中声明数组的方式如下: datatype[] arrayRefVar; 或者 datatype arrayRefVar[]; 例如: double[] array; 或者 double array[]; 说明:我