欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java day_03

程序员文章站 2024-02-10 09:19:52
...

java

跳转控制语句
break:退出当前循环

package demo_02;
import java.util.Scanner;
public class Control_demo_01 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the number of lines:");
        int lines = sc.nextInt();
        System.out.println("Please enter the number of columns:");
        int colums = sc.nextInt();
//        for循环的外层是代表的行,而内层是列。外层每执行一次是一行,一行由多个内层循环的结果组成。
        for (int i = 0 ; i < lines ; i++ ){
            for (int j = 0 ; j < colums ; j++ ){
                if (i==3){
                    break;
                }
                System.out.print("#");
            }
            //            为了换行使用
            System.out.println();
        }
    }
}

continue:退出本次循环

package demo_02;

public class Control_demo_02 {
    public static void main(String[] args){
//只输出奇数
        for (int i = 0 ; i < 10 ; i++ ){
            if(i % 2 == 0){
//                如果是偶数那么不会进行本次循环
                continue;
            }
            System.out.println(i);
        }
    }
}

return:结束当前方法

package demo_02;
//如果运行会输出一个1,因为continue会在i= 0的时候跳过return
public class Control_demo_02 {
    public static void main(String[] args){
//只输出奇数
        for (int i = 0 ; i < 10 ; i++ ){

            if(i % 2 == 0){
//                如果是偶数那么不会进行本次循环
                continue;
            }
            System.out.println(i);
            return;
            //这里return会让循环直接退出并且退出main方法!
        }
    }
}

方法概述

格式:
修饰名 返回值类型 方法名 (参数类型 参数名,参数类型 参数名…)
{
函数体;
return 返回值;
}

方法重载
方法名相同,参数列表个数不同 ,参数类型不同,参数类型不同,都可以使用方法的重载。

package demo_02;
import java.util.Scanner;
public class Method_overloading {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int sum = add(a,b);
        System.out.println(sum);
    }
    public static int add(int a , int b){
        return a+b;
    }
    public  static int add (int a , int b ,int c){
        return a+b+c;
    }
    public static int add(byte a , byte b ){
//        这里之所以会不报错是因为int比byte大,所以会自动合并。
        return a+b;
    }
    public static int add(byte a , int b){
        return a + b;
    }
    public  static int add (int b ,byte a ){
        return a + b ;
    }
}

数组

数组基本格式:
数据类型[] 数组名 = new 数据类型[数组长度];
int [] a = new int [100];

package demo_02;

public class Array_demo_01 {
    public static void main(String[] args) {
        int [] a  = new int [100];
        for (int i = 0 ; i < 100 ; i++){
            a[i]= i;
        }
        System.out.println(a[50]);
    }
}
package demo_02;

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

    }