数组和方法

数组定义(静态初始化):

int[] a={0,1,2,3,4,5};              // 定义一个int数组              方法1

int[] a=new int[]{1,2,3,4,5};       // 创建一个int类型的数组对象      方法2

double[] b={1.1,2.6,3.5,4.0,5.1};   // 定义一个double类型的数组

String[] n={"张三",“李四”,“王五”};

数组打印:

package demon;

import java.util.Arrays;

public class d1 {
    public static void main(String[] args) {
        Double[] a=new Double[]{1.1,2.3,3.0,4.0,5.0};
        System.out.println(Arrays.toString(a));        // Arrays跟Array不一样,打印字符只能用Arrays.toString
    }
}

//  直接打印int数组      System.out.println(a);    [I@4554617c                  
//  直接打印Double数组   System.out.println(a);    [Ljava.lang.Double;@4554617c
//  [ : 表示当前是一个数组
//  I : 表示当前是int类型
//  @ : 表示间隔符号(固定类型)
//  4554617c : 数组真正的地址

[1.1, 2.3, 3.0, 4.0, 5.0]

数组的遍历

package demon;
public class d1 {
    public static void main(String[] args) {
        int[] a={1,2,3,35,47,51,65,73,28,92,96};
        System.out.print("["+"\t");
        for (int i = 0; i < a.length; i++) {       // 利用for循环将数组进行打印
            System.out.print(a[i]+"\t");
        }
        System.out.print("]");
    }
}


[	1	2	3	35	47	51	65	73	28	92	96	]

案例:统计个数

/* 定义一个数组,存储1,2,3,4,5,6,7,8,9,10
遍历数组得到每一个元素,统计数组里面一共多少个能被3整除的数字*/

package demon;
public class d1 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6,7,8,9,10};
        int sum=0;
        for (int i = 0; i < a.length; i++) {      // i表示索引
            if (a[i]%3==0){
                sum++;
            }
        }
        System.out.println(sum);
    }
}

3

案例:奇偶变化

// 如果是奇数扩大两倍,偶数缩小两倍

package demon;
public class d1 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6,7,8,9,10};
        int sum=0;
        for (int i = 0; i < a.length; i++) {
            if (a[i]%2==0){
                a[i]=a[i]/2;
            }else {
                a[i]=a[i]*2;
            }
            System.out.print(a[i]+"\t");
        }
        
        // for (int i = 0; i < a.length; i++) {
        // System.out.print(a[i]+"\t");  
        // 可以不写这句代码System.out.print(a[i]+"\t");直接写上方两句代码
        
    }
}


2	1	6	2	10	3	14	4	18	5
    
// 静态初始化

数组定义(动态初始化):

int[] a=new int[5];    // 定义一个int数组,数组有5个元素
    
// 整数,小数类型默认值是:0和0.0     引用类型默认值是:null     boolean默认值是:false    字符类型默认值是:'\u0000'空格


案例:求最值

package demon;

public class d2 {
    public static void main(String[] args) {
        int[] a={2,45,7,39,20,30};
        int max=a[0];                                  //int max不能等于0只能是数组中第一个元素
        for (int i = 1; i < a.length; i++) {
            if (max<a[i]){
                max=a[i];
            }
        } 
        System.out.println(max);
    }
}


45

案例:随机数求(和,平均,最小数)

/*生成1-100之间的随机数存入数组
1)求所有数据和
2)求数据平均数
3)统计多少个数据小于平均数*/

package demon;

import java.util.Random;

public class d2 {
    public static void main(String[] args) {
        Random r=new Random();
        int sum=0;
        int average=0;
        int small=0;
        for (int i = 0; i < 10; i++) {
            int a=r.nextInt(100)+1;
            System.out.print(a+"\t");
            sum=sum+a;
            average=sum/10;
            if (a<average){
                small++;
            }
        }
        System.out.println();
        System.out.println("总和"+sum);
        System.out.println("平均数"+average);
        System.out.println("小于平均数的个数"+small);
    }
}


53	73	48	46	46	11	99	10	77	90	
总和553
平均数55
小于平均数的个数2
    
// 方法1    直接引用不定义数组
/*生成1-100之间的随机数存入数组
1)求所有数据和
2)求数据平均数
3)统计多少个数据小于平均数*/

package demon;

import java.util.Random;

public class d {
    public static void main(String[] args) {
    int[] a=new int[10];
    int sum=0;
    int average=0;
    int small=0;
    Random m =new Random();
        for (int i = 0; i < a.length; i++) {
            int b=m.nextInt(100)+1;
            a[i]=b;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+"\t");
            sum+=a[i];
            average=sum/10;
            if (average>a[i]){
                small++;
            }
        }
        System.out.println();
        System.out.println("总和"+sum);
        System.out.println("平均数"+average);
        System.out.println("小于平均数的个数"+small);

}
}


56	41	70	94	32	6	25	22	44	47	
总和437
平均数43
小于平均数的个数3
    
// 方法2   定义数组进行引用(老师用的方法)

案例:交换两变量代码

package demon;

public class d3 {
    public static void main(String[] args) {
        int a=10;
        int b=20;
        int temp=0;
        temp=a;
        a=b;
        b=temp;
        System.out.println(a);
        System.out.println(b);
    }
}


20
10
//将定义的数组1,2,3,4,5转换成5,2,3,4,1

package demon;

public class d3 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5};
        int temp=0;
        temp=a[0];
        a[0]=a[4];
        a[4]=temp;
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+"\t");
        }
    }
}

     
5	2	3	4	1	

冒泡排序

package demon;

public class d5 {
    public static void main(String[] args) {
        int[] a={1,6,4,9,2};
        int temp=0;
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if (a[j]>a[j+1]){
                   temp=a[j];
                   a[j]=a[j+1];
                   a[j+1]=temp;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+"\t");
        }
    }
}



1	2	4	6	9

实参

getSum(20,30);

形参

getSum(int num1,int num2);

直接调用(无返回值时使用)--在定义方法中进行sout输出

package demon;

public class d3 {
    public static void main(String[] args) {
        hello(10, 20);                        // 直接定义参数,在定义的方法中进行输出
    }
    public static void hello(int a, int b) {
        int m = a + b;
        System.out.println(m);                // 在此方法中输出
    }
}


30

赋值调用(有返回值时使用)--需要sout进行输出

package demon;

public class d3 {
    public static void main(String[] args) {
        int ko=hello(10,20);                    // 先定义参数
        System.out.println(ko);                 // 再进行输出
    }
    public static int hello(int a,int b) {
        int m=a+b;
    return m;
    }
}


30

输出调用 --在输出的时候定义参数

package demon;

public class d3 {
    public static void main(String[] args) {
        System.out.println(hello(10,20));       // 在输出的时候定义参数
    }
    public static int hello(int a,int b) {
        int m=a+b;
    return m;
    }
}


30

案例:判断长方形周长大小

package demon;

public class d3 {
    public static void main(String[] args) {
        int l=hello(6,9);                         // 将方法调用和判断
        int m=hello(10, 20);
        if (l>m){
            System.out.println(l+"大于"+m);
        }
        else {
            System.out.println(m+"大于"+l);
        }
    }
    public static int hello(int a, int b) {       // 进行方法的定义
        int m = (a + b)*2;
        return m;
    }
}


60大于30

方法注意事项

  • 方法不调用不会执行
  • 方法不能嵌套使用
  • 方法执行顺序与编写顺序无关,先调用先执行
  • 除了void,final,private不需要写返回值return(结束方法),其他类都需要有返回值
  • return下面不能写其他代码,永远执行不到

方法重载

同一类中方法名相同但参数不同的方法体

package demon;

public class d3 {                                                 // 类的定义
    public static void main(String[] args) {                      // 运行环境             
        
    }
    public static int hello(int a, int b) {                       // 方法的定义
        int m = (a + b) * 2;
        return m;
    }
    public static int hello(int a, int b, int c) {
        int m = (a + b + c) * 2;
        return m;
    }
    public static int hello(int a, int b, int c, int d) {
        int m = (a + b + c + d) * 2;
        return m;
    }
}

![623ca47351610fbfb9984382b8e20e9](D:\WeChat Files\wxid_6no8nh20sfwy22\FileStorage\Temp\623ca47351610fbfb9984382b8e20e9.jpg)

强转调用

package demon;

public class d3 {
    public static void main(String[] args) {
        hello((short) 10,(short)20);                // 强转调用
    }
    public static void hello(int a, int b) {
        int m = (a + b) * 2;
        System.out.println("int");
    }
    public static void hello(byte a, byte b) {
        int m = (a + b ) * 2;
        System.out.println("byte");
    }
    public static void hello(short a, short b) {
        int m = (a + b) * 2;
        System.out.println("short");
    }
}


short
    
    // 方法1
package demon;

public class d3 {
    public static void main(String[] args) {
        short d=10;                                // 定义及调用
        short o=20;
        hello(d,o);
    }
    public static void hello(int a, int b) {
        int m = (a + b) * 2;
        System.out.println("int");
    }
    public static void hello(byte a, byte b) {
        int m = (a + b ) * 2;
        System.out.println("byte");
    }
    public static void hello(short a, short b) {
        int m = (a + b) * 2;
        System.out.println("short");
    }
}


short
    
    // 方法2

案例:数组遍历

// 打印数组{	1	2	3	4	5	}

package demon;

public class d3 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5};
        hello(a);
    }
    public static void hello(int[] a) {
        System.out.print("{"+"\t");
        for (int i = 0; i <a.length; i++) {
            System.out.print(a[i]+"\t");
        }
        System.out.print("}");
    }
}



{	1	2	3	4	5	}

案例;数组最大值

package demon;

public class d3 {
    public static void main(String[] args) {
        int[] a={4,2,6,9,10};
        hello(a);
    }
    public static void hello(int[] a) {
        int v=a[0];
        for (int i = 1; i < a.length; i++) {
            if (v<a[i]){
                v=a[i];
            }
        }
        System.out.println(v);
    }
}


10
    
    // 无返回值
package demon;

public class d3 {
    public static void main(String[] args) {
        int[] a={4,2,6,9,10};
        int i=hello(a);
        System.out.println(i);
    }
    public static int hello(int[] a) {
        int v=a[0];
        for (int i = 1; i < a.length; i++) {
            if (v<a[i]){
                v=a[i];
            }
        }
    return v;}
}


10
    
    // 有返回值

案例:判断查找数是否在数组中

package demon;
public class d1 {
    public static void main(String[] args) {
        int[] a={1,6,6,9,29,10};
        int b=6;
        boolean b1 = judgeA(a, b);
        System.out.println(b1);
    }
    public static boolean judgeA(int[] a,int b) {
        for (int i = 0; i < a.length; i++) {
            if (a[i]==b){
                return true;
            }
        }
    return false;}
}


true
    
    // 有返回值类型 ***
package demon;
public class d1 {
    public static void main(String[] args) {
        int[] a={1,6,6,9,29,10};
        int b=6;
        judgeA(a, b);
    }
    public static void judgeA(int[] a,int b) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == b) {
                System.out.println("有数值匹配");
                break;
            } else {
                System.out.println("数值不匹配");
                break;
            }
        }
    }
}


数值不匹配
    
    //无返回值类型

return跟break:

案例:复制数组

/*定义一个方法a(int[] b,int from,int to)
将b数组中第form到to索引的数组打印并返回*/

package demon;

public class d1 {
    public static void main(String[] args) {
        int[] a = {1, 6, 6, 9, 29, 10};
        judgeA(a, 2, 4);
    }
    public static void judgeA(int[] a, int b, int c) {
        for (int i = b ; i < c + 1; i++) {
            System.out.println(a[i]);
        }
    }
}


6
9
29
    
    // 直接调用***
package demon;

public class d1 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6,7,8,9};
        copyRang(a,2,7);
    }
    public static void copyRang(int[] a,int b,int c){
        int[] arr=new int[c-b+1];
        int d=0;
        for (int i = b; i < c+1; i++) {
            arr[d]=a[i];
            System.out.print(arr[d]+"\t");             // 打印arr[d]
            d++;                                       // d++跟上一行代码顺序不能调换,会报错   
        }
    }
}


3	4	5	6	7	8	
    
    // 通过定义数组直接调用****
package demon;

public class d1 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6,7,8,9};
        int[] copied = copyRang(a, 2, 7);
        for (int i = 0; i < copied.length; i++) {      //在输出循环语句是必须要先进行遍历
            System.out.print(copied[i]+"\t");
        }
    }
    public static int[] copyRang(int[] a,int b,int c){
        int[] arr=new int[c-b+1];
        int d=0;
        for (int i = b; i < c+1; i++) {
            arr[d]=a[i]; 
            d++;
        }
    return arr;
    }
}


3	4	5	6	7	8	
    
    // 赋值调用****
  • return(结束方法返回结果)跟方法有关,方法中执行到return该方法就终止

基本数据类型和引用数据类型

        int a=10;
        int b=a;                // 传递的是真实数据

        //基本数据类型,在栈里面,不容易改变
        
        int[] arr1={1,2,3,4,5};
        int[] arr2=arr1;        // 传递的是地址值

        //引用数据类型,在堆里面,容易改变

基本数据类型传递:

package demon;

public class d1 {
    public static void main(String[] args) {
        int a=100;
        System.out.println(a);
        change(a);
        System.out.println(a);
    }
    public static void change(int a){
        a=200;
    }
}


100
100
package demon;

public class d1 {
    public static void main(String[] args) {
        int a=100;
        System.out.println(a);
        int change = change(a);
        System.out.println(change);
    }
    public static int change(int a){
        a=200;
    return a;}
}


100
200

传递基本数据类型,传递真实数据时,无返回值的形参改变不影响实际参数值

通过给返回值的方式可以强行改变数据类型

看有没有new——new了方法里变量改变调用值就会变

案例:机票打折

package demon;

import java.util.Scanner;

public class d1 {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        System.out.println("输入月份");
        int b =a.nextInt(12);
        System.out.println("输入钱数");
        int j =a.nextInt();
        edisPort(b,j);
        mdisPort(b,j);
    a.close();}
    public static void edisPort(int b,double j) {
        if (b > 4 && b < 11 ) {
            if (j == 500) {
                j = j * 0.9;
            } else {
                j = j * 0.85;
            }
            System.out.println(j);
        }
    }
    public static void mdisPort(int b,double j){
        if ((a <= 4 && a >= 1)||(a <= 12)){
           if (j==500){
               j=j*0.7;
           }else {
               j=j*0.65;
           }
        }
        System.out.println(j);
    }
}
package demon;

import java.util.Scanner;

public class d1 {
    public static void main(String[] args) {
        Scanner m = new Scanner(System.in);
        System.out.println("输入月份");
        int a = m.nextInt();
        System.out.println("输入钱数");
        Double b = m.nextDouble();
        System.out.println("输入舱数,0代表头等舱,1代表经济舱");
        int c = m.nextInt();
        if (a <= 11 && a >= 5) {
            hello(a, b, c);
        }
        else if ((a <= 4 && a >= 1)||(a <= 12))  {
                hello1(a, b, c);
            }
        else {
            System.out.println("输入错误");
        m.close();}
        }
        public static void hello ( int a, Double b,int c){
            if (c == 0) {
                b = b * 0.9;
            } else {
                b = b * 0.85;
            }
            System.out.println(b);
        }
        public static void hello1 ( int a, Double b,int c){
            if (c == 0) {
                b = b * 0.7;
            } else {
                b = b * 0.65;
            }
            System.out.println(b);
        }
    }



输入月份
5
输入钱数
600
输入舱数,0代表头等舱,1代表经济舱
0
540.0

案例:101到200之间质数个数

package ko;

public class light {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 101; i < 201; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i+"\t");
                sum++;
            }
            if (i % 50 == 0) {
                System.out.println();
            }
        }
        System.out.println("一共有"+sum+"个质数");
    }
}



101	103	107	109	113	127	131	137	139	149	
151	157	163	167	173	179	181	191	193	197	199	
一共有21个质数

案例:验证码

package ko;

import java.util.Random;

public class d3 {
    public static void main(String[] args) {
        Random r = new Random();
        int a = r.nextInt(26)+65;
        int b = r.nextInt(26)+65;
        int c = r.nextInt(26)+97;
        int d = r.nextInt(26)+97;
        int e = r.nextInt(10);
        char f = (char) a;
        char g = (char) b;
        char h = (char) c;
        char i = (char) d;
        System.out.println(f+""+g+""+h+""+i+""+e);
    }
}


YNwb0
package demon;

import java.util.Random;

public class d5 {
    public static void main(String[] args) {
        Random r = new Random();
        int a = r.nextInt(26)+65;              // 自动生成大写字母
        int b = r.nextInt(26)+65;
        int c = r.nextInt(26)+97;              // 自动生成小写字母
        int d = r.nextInt(26)+97;
        int e = r.nextInt(10);                 // 生成0-9之间的数字
        extracted(a,b,c,d,e,r);
    }
    public static void extracted(int a,int b,int c,int d,int e,Random r) {
        char f = (char) a;
        char g = (char) b;
        char h = (char) c;
        char i = (char) d;
        System.out.println(f+""+g+""+h+""+i+""+e);
    }
}


KUzf4

案例:评委打分***

package demon;

import java.util.Scanner;

public class d7 {
    public static void main(String[] args) {
        Scanner m = new Scanner(System.in);
        int[] a = new int[6];
        int judge = 0;
        while (judge < a.length) {
            System.out.println("输入你心目中的分数");
            int peJu = m.nextInt();
            if (peJu <= 100 && peJu >= 0) {
                a[judge] = peJu;
                judge++;
            } else {
                System.out.println("输入错误");
                continue;
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");           //输出谁[]括号就写谁,这里定义是i不是judge
        }
        m.close();
    }
}



输入你心目中的分数
50
输入你心目中的分数
60
输入你心目中的分数
40
输入你心目中的分数
30
输入你心目中的分数
20
输入你心目中的分数
10
50 60 40 30 20 10 
package demon;

import java.util.Scanner;

public class d7 {
    public static void main(String[] args) {             // 定义输出评委打分
        Scanner m = new Scanner(System.in);
        int[] a = new int[6];
        int judge = 0;
        while (judge < a.length) {
            System.out.println("输入你心目中的分数");
            int peJu = m.nextInt();
            if (peJu <= 100 && peJu >= 0) {
                a[judge] = peJu;
                judge++;
            } else {
                System.out.println("输入错误");
                continue;
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        m.close();
        
        int max=getMax(a);                                // 方法调用打印
        int min=getMin(a);
        int sum=getSum(a);
        System.out.println();
        System.out.println(sum-(max+min));
        System.out.println((sum-(max+min))/(a.length-2));

    }
    
    public static int getMax(int[] a){                    // max方法
        int max=a[0];
        for (int i = 1; i < a.length; i++) {
            if (max<a[i]){
                max=a[i];
            }
        }
        return max;
    }
    
    public static int getMin(int[] a){                    // min方法
        int min=a[0];
        for (int i = 1; i < a.length; i++) {
            if (min>a[i]){
                min=a[i];
            }
        }
        return min;
    }
    
    public static int getSum(int[] a){                    // sum方法
        int sum=0;
        for (int i = 0; i < a.length; i++) {
            sum+=a[i];
        }
        return sum;
    }
}


输入你心目中的分数
10
输入你心目中的分数
90
输入你心目中的分数
50
输入你心目中的分数
50
输入你心目中的分数
50
输入你心目中的分数
50
10 90 50 50 50 50 
200
50
    
    // *****

数字加密***

package demon;

import java.util.Scanner;

public class d5 {
    public static void main(String[] args) {
        Scanner S=new Scanner(System.in);       // 定义输入值
        System.out.println("输入数字");
        int a=S.nextInt();
        code(a);

    S.close();}

    public static void code(int a) {             // 定义取出末尾数的方法
        int[] b=new int[4];
        int ge=0;
        for (int i = 0; i <b.length; i++) {
            ge=((a%10)+5)%10;                     // 取出a结尾的数
            b[i]=ge;
            a/=10;                                // 一定不要忘记更新输入的值
        }
        for (int i = 0; i < b.length; i++) {      // 正序遍历数组
            System.out.print(b[i]+"");
        }
    }
}


输入数字
1234
9876

数字解密***

package demon;

import java.util.Scanner;

public class d5 {
    public static void main(String[] args) {
        Scanner S=new Scanner(System.in);
        System.out.println("输入数字");
        int a=S.nextInt();
        code(a);

    S.close();}

    public static void code(int a) {              // 定义取出末尾数的方法
        int[] b=new int[4];
        int ge=0;
        for (int i = 0; i <b.length; i++) {           
            ge=((a%10)+5)%10;                     // 取出a结尾的数               
            b[i]=ge;
            a/=10;                                // 一定不要忘记更新输入的值                                     
        }
        for (int i = b.length-1; i >=0; i--) {    // 倒序遍历数组
            System.out.print(b[i]+"");
        }
    }
}


输入数字
8710
3265

加密和解密用的公式都是一样的,只有最后遍历的时候不一样,一个是正序一个是倒序

案例:抽奖********

package demon;

import java.util.Random;

public class d5 {
    public static void main(String[] args) {
       int[] arr={2,588,888,1000,10000};                   // 定义数组
       int[] newArr=new int[arr.length];                   // 定义新数组用于存储抽奖结果
        Random r = new Random();
        for (int i = 0; i < 5; i++) {
            int rute=r.nextInt(arr.length);
            int price=arr[rute];
            boolean flage = contanis(newArr,price);
            if(!flage){
                newArr[i]=price;
                i++;
            }
        }
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]);
        }
    }
    public static boolean contanis(int[] arr, int price) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == price) {
                return true;
            }
        }
        return false;
    }
}



2
0
10000
0
588

案例:彩票(未完待续)

package demon;

import java.util.Random;
import java.util.Scanner;

public class d5 {
    public static void main(String[] args) {
        Random r=new Random();
        Scanner m=new Scanner(System.in);
        int[] b=new int[6];
        for (int i = 0; i <6; i++) {
            int a=r.nextInt(31)+1;
            b[i]=a;
            System.out.print(b[i]+" ");
        }
        int c=r.nextInt(16)+1;
        System.out.println(c);

        int[] inPut=new int[6];
        for (int i = 0; i < b.length; i++) {
            System.out.println("输入数字");
            int d=m.nextInt();
            inPut[i]=d;
        }
        int right=m.nextInt();

        boolean ui=judge(b,inPut);
        if (ui==true){
            System.out.println("中奖");
        }
    }
    public static boolean judge(int[] b, int[] inPut){
        boolean flage=true;
        int sum=0;
        for (int i = 0; i < 6; i++) {
            if (b[i]==inPut[i]){
                flage=true;
                sum++;
            }
        }
    return false;}
}    

二维数组遍历:

public class d1 {
    public static void main(String[] args) {
        int[][] a = {
                {1, 2, 3},
                {4, 5, 6, 7, 8, 9, 10}
        };
        for (int i = 0; i< a.length; i++) {              // 遍历行数(一维数组)
            for (int j = 0; j < a[i].length; j++) {      // 遍历列数中每个元素(一维数组中的每个元素)
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
}


1 2 3 
4 5 6 7 8 9 10 
数据类型[][] 数组名=new 数据类型[m][n];        //m表示行 ,n表示列
package ko;

public class d1 {
    public static void main(String[] args) {
        int[][] a =new int[3][5];
        a [1][0]=2;
        a [1][1]=6;
        a [1][2]=2;
        a [1][3]=5;
        a [1][4]=2;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]);
            }
            System.out.println();
        }
    }
}


00000
26252
00000

案例:计算营业额和季度营业额

package ko;

import java.util.Scanner;

public class d1 {
    public static void main(String[] args) {
        int[][] a = new int[4][3];                         // 定义一个4行3列的数组
        Scanner m = new Scanner(System.in);                // 调用输入方法
        
        int allSum = 0;                        // 定义一个全部总和的初始值(全部总和的值必须放在循环的外面)
        
        for (int i = 0; i < a.length; i++) {
            
            int sum=0;                         // 定义季度总和的初始值(季度总和的值必须放在循环的里面)
            
            for (int j = 0; j < a[i].length; j++) {
                System.out.print("输入数字 : ");
                int b = m.nextInt();                       // 调用输入的数值
                a[i][j] = b;
                sum+=a[i][j];
                allSum+=a[i][j];
            }
            System.out.println();
            System.out.println("第 " + (i + 1) + " 季度的总和: " + sum);  // 输出当前季度的总和
        }
        m.close();
        System.out.println(allSum);
    }
}


输入数字 : 1
输入数字 : 1
输入数字 : 11 季度的总和: 3
输入数字 : 1
输入数字 : 1
输入数字 : 12 季度的总和: 3
输入数字 : 1
输入数字 : 1
输入数字 : 13 季度的总和: 3
输入数字 : 1
输入数字 : 1
输入数字 : 14 季度的总和: 3
12
    
// 动态定义数组并进行计算

快捷键

CTRL+ALT+L:重置格式

Ctrl+ALT+V:快速创建对象

Ctrl+ALT+M:快速抽取方法(选中要抽取的语句)

Shift+F6:批量修改相同字符

Ctrl+P:查看补齐的变量参数

Ctrl+Alt+T:自动匹配代码块

posted @   FoxDusk  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示