2018-04-09Java循环嵌套

for循环中再嵌套for循环,也可嵌套while或者do···while循环,三种循环可以相互循环;外部循环变量影响内部的循环,且变量不能同名,外循环必须完全包含内循环,不能有交叉循环,在多层循环中,一个break语句只向外跳一层

package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                System.out.println(i+"-"+j);
            }
        }
    }
}

1-1
1-2
1-3
1-4
1-5
2-1
...
5-3
5-4
5-5
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for(int i=1;i<=5;i++){
            for(int j=1;j<=i;j++){//j<=i;变量变换
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

*
**
***
****
*****
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

1*1=1   
1*2=2   2*2=4   
1*3=3   2*3=6   3*3=9   
1*4=4   2*4=8   3*4=12  4*4=16  
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  

package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("num:");
        int num=input.nextInt();
        char ch='A';
        for(int i=1;i<=num;i++){
            for(int j=1;j<=i;j++){
                System.out.print(ch);
            }
            System.out.println();
        }
    }
}
num:5
A
AA
AAA
AAAA
AAAAA
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("num:");
        int num=input.nextInt();
        for(int i=num;i>=1;i--){
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
num:5
*****
****
***
**
*
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("num:");
        int num=input.nextInt();
        for(int i=1;i<=num;i++){
            for(int j=1;j<=num;j++){//先画一个矩形
                if(j<=num-i){//两边找规则去掉
                    System.out.print("o");//把o换成空格
                }else{
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
}
num:5
oooo*
ooo**
oo***
o****
*****

num:5
    *
   **
  ***
 ****
*****
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("num:");
        int num=input.nextInt();
        for(int i=1;i<=num;i++){
            for(int j=1;j<=2*num-1;j++){//先画一个矩形
                if(j<=num-i||j>=num+i){//两边找规则去掉
                    System.out.print("o");//把o换成空格
                }else{
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
}
num:5
oooo*oooo
ooo***ooo
oo*****oo
o*******o
*********
num:5
    *    
   ***   
  *****  
 ******* 
*********

数组

1,声明数组--数组类型[] 数组名;
2,开辟数组空间-数组名=new数组类型[数组长度];
3,数组原数赋值-数组名[数组下标]=值;
4,使用数组元素;
int[] a;//声明数组---告诉计算机数据是什么类型
a=new int[5];//开辟空间---告诉计算机分配几个格子
a[0]=1;//赋值---分配格子放入数据
特殊写法
声明赋值--数组类型[] 数组名=[new 数组类型[]]]{值....}

int[] a ;
a=new int[5];
a[0]=1;

特殊写法

int[] a={1,2,3,4,5,6};
int[] a;
a=new int[]{1,2,3,4,5,6};
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a = new int[5];
        for(int i=0;i<5;i++){
        System.out.print("a["+i+"]:");
        a[i]=input.nextInt();
        }
        for(int i=0;i<5;i++){
            System.out.println(a[i]);
        }
    }
}
a[0]:1
a[1]:2
a[2]:3
a[3]:4
a[4]:5
1
2
3
4
5
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a;
        a = new int[5];
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            System.out.print("第" + (i + 1) + "位学员成绩:");
            a[i] = input.nextInt();
            sum += a[i];
        }
        System.out.println("平均分:" + (double) sum / a.length);

    }
}
第1位学员成绩:100
第2位学员成绩:90
第3位学员成绩:80
第4位学员成绩:70
第5位学员成绩:60
平均分:80.0
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a;
        a=new int[]{8,4,2,1,23,344,12};
        int sum=0;
        System.out.print("num:");
        int num=input.nextInt();
        boolean flag=false;
        for(int i=0;i
输入一个数看是否在元素值中
num:32
84212334412不存在
package miao;

import java.util.Scanner;

public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a = new int[] { 2800, 2900, 2750, 3100 };
        int min = a[0];
        for (int i = 1; i < a.length; i++) {
            if (min > a[i]) {
                min = a[i];
            }
        }
        System.out.println("四个数中:"+min);
    }

}
四个数中:2750

增强型for循环

for(变量类型 变量名:列表名){
通过变量名变量列表中的每一个元素
}

int[] a = new int[] { 2800, 2900, 2750, 3100 };
for (int b : a) {//变量b中赋值了变量a的所有元素值
System.out.println(b);
}
2800
2900
2750
3100
int[] [] a=new int[3][5];
int[] [] b={{1,2,3},{4,5}};//
int[] [] c=new int[3][];
c[0]=new int[5];
c[1]=new int[4];
c[2]=new int[3];
package miao;
import java.util.Scanner;
public class Miao {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a = { 2, 4, 23, 1 };
        for (int element : a) {
            System.out.print(element + " ");
        }
        for (int i = a.length - 1; i >= 1; i--) {
            for (int j = 0; j < i; j++) {
                if (a[j] > a[j + 1]) {
                    a[j] = a[j] + a[j + 1];
                    a[j + 1] = a[j] - a[j + 1];
                    a[j] = a[j] - a[j + 1];
                }
            }
        }
        System.out.println();
        for (int element : a) {
            System.out.print(element + " ");
        }
    }
}
2 4 23 1 
1 2 4 23  //排序

类类型

Arrays//import java.util.Arrays;
toString(数组名)返还是String;

int[] a = { 2, 4, 23, 1 };
string s=Arrary.toString(a);
System.out.println(s);
[2,4,23,1]//把int整数型变成字符串形式输出

equals(数组名1,数组名2)返还boolean;

int[] a = { 2, 4, 23, 1 };
int[] b = { 2, 4, 23, 1 };
boolean flag=Arrary.equals(a,b);
System.out.println(flag);
ture//把ab中的元素值比较

sort(数组名);直接排序

int[] a = { 2, 4, 23, 1 };
int[] b = { 4, 2, 23, 1 };
System.out.println(Arrary.sort(b));
Arrary.sort(b);
System.out.println(Arrary.sort(b));
[4,2,23,1]
[2,4,23,1]//元素值排序

binarySearch(数组名,查找元素)返回int(数组下标)

int[] a = { 2, 4, 23, 1 };
int[] b = { 4, 2, 23, 1 };
int i=Arrary.binarySearch(a,2);
System.out.println(i);
0

copuof(数组名,长度)返回数组类型;

int[] a = { 2, 4, 23, 1 };
int[] b=Arrays.copuof(a,a.length);
System.out.println(Arrary.tostring(b));
[2, 4, 23, 1]

你可能感兴趣的:(2018-04-09Java循环嵌套)