JAVA基础学习day02---程序结构与控制、函数与数组入门

一、程序结构

1、顺序结构

2、选择结构

3、循环结构

二、顺序结构

程序至上而下逐行执行,一条语句执行完之后继续执行下一条语句,一直到程序的末尾

三、条件选择结构

选择结构是根据条件的成立与否,再决定要执行哪些语句的一种结构

3.1 、IF语句--单分支结构

if (判断条件){

语句1 ;

public class IFTest1 {
    public static void main(String[] args) {
        int x=3;
        int y=10;
        System.out.println("====开始比较=====");
        if(x>y){
            System.out.println("x比y大!");
        }
        if(x<y){
            System.out.println("x比y小!");
        }
        if(x==y){
            System.out.println("x等于y!");
        }
        System.out.println("====比较完成!=====");
    }
}

2、if...else语句--双分支结构

if (判断条件){

语句主体1 ;

}else{

语句主体2;

}

public class IFELSETest {
    public static void main(String[] args) {
        int x = 3;
        if (x % 2 == 0) {
            System.out.println(x + "是偶数!");
        } else {
            System.out.println(x + "是奇数!");
        }
    }
}

3.3、if…else if…else语句 --多分支结构

如果需要在if..else里判断多个条件时,就需要if..else if … else语句了,其格式如下:

if (条件判断1){

语句主体1 ;

}else if (条件判断2){

语句主体 2 ;

}

… // 多个else if()语句

else{

语句主体3 ;

}

public class test6 {
    public static void main(String[] args) {
        int x=3;
        if(x==1){
            System.out.println("x的值是1!");
        }else if(x==2){
            System.out.println("x的值是2!");
        }else if(x==3){
            System.out.println("x的值是3!");
        }else{
            System.out.println("x的值不是1,2,3 中的一个!");
        }
    }
}

四、Switch结构

要在许多的选择条件中找到并执行其中一个符合判断条件的语句时,除了可以使用if..else不断地判断之外,也可以使用另一种更方便的方式即多重选择——switch语句,语法格式:

switch (表达式){

case 选择值1 : 语句主体 1 ;        break ;

case 选择值2 : 语句主体 2 ;                 break ;

…….

case 选择值n : 语句主体 n ;                break ; default:  语句主体 ;

}

4.1、正常运行的Switch

public class SwitchTest {

        public static void main(String[] args) {
                //定义扫描器变量
                Scanner scanner=new Scanner(System.in);
                //定义变量接收
                int num=scanner.nextInt();
                /**
                 * 数值类型的结果,并不是很多时用switch
                 */
                 switch(num){  //byte short int char只接收4种类型  JDK7以上switch可以判断字符串
                         case 3:
                         case        4:
                         case 5:
                                 System.out.println(num+"春季");
                                 break;
                         case 6:
                         case        7:
                         case 8:
                                 System.out.println(num+"夏季");
                                 break;
                         case 9:
                         case        10:
                         case 11:
                                 System.out.println(num+"秋季");
                                 break;
                         case 12:
                         case        1:
                         case 2:
                                 System.out.println(num+"冬季");
                                 break;
                         default:
                                 System.out.println("输入 错误,请输入1--12之间的数字");
                 }

        }

}

4.2、没有Break的Switch

public class SwitchTest2 {

        public static void main(String[] args) {
                int a = 10;
                int b = 3;
                char c = '+';

                switch (c) {
                default:
                        System.out.println("未知操作符");
                case '+':
                        System.out.println("a+b=" + (a + b));
                case '-':
                        System.out.println("a-b=" + (a - b));
                case '*':
                        System.out.println("a*b=" + (a * b));
                case '/':
                        System.out.println("a/b=" + (a / b));

                }

        }

}

五、循环while和do..while

5.1、While循环

/**
 * 使用While进行100以内累加操作
 *
 */
public class WhileTest {

    public static void main(String[] args) {
        
        int i=1;  //变量
        int sum=0; //和
        
        while(i<=100){//先判断条件
            sum+=i;
            i++;
        }
        System.out.println("100以内整数之和:"+sum);

    }

}


5.2 do...while

/**
 * 使用do...While进行100以内累加操作
 * 
 *
 */
public class DoWhileTest {

    public static void main(String[] args) {
        int i = 1; // 变量
        int sum = 0; // 和

        do {
            sum += i;
            i++;

        } while (i <= 100);

        System.out.println("100以内整数之和:" + sum);
    }

}


5.3、while和do..while区别

public class WhileDemo {

        public static void main(String[] args) {
                //while 判断条件可能一次也不执行
                int x=1;
                
                while(x<3);
                {
                        System.out.println("x="+x);
                        x++;
                }
                
                //do while 最少执行一次
                int y=1;
                
                do{
                        System.out.println("do:y="+y);
                        y++;
                }while(y<3);

        }

}

六、for循环

 6.1、for循环基本结构

for (赋值初值;判断条件;赋值增减量){

语句1 ;

….

语句n ;

}

6.2、使用for进行100以内累加操作

/**
 * 使用for进行100以内累加操作
 *
 */
public class ForTest {

    public static void main(String[] args) {
        int sum=0;
        for (int i = 0; i <=100; i++) {
            sum+=i;
        }
        System.out.println("100以内整数之和:" + sum);
    }

}


6.3、打印99乖法表

双循环打印
/**
 * 打印99乖法表
 * 
 *
 */
public class Demo7 {

        public static void main(String[] args) {
                //外层循环控制行数
                for(int i=1;i<=9;i++){
                        //内层循环控制列
                        for (int j = 1; j <=i; j++) {
                                System.out.print(j+"*"+i+"="+(j*j)+"  ");
                        }
                        //换行
                        System.out.println();
                }

        }

}

一个循环打印

/**
 * 打印99乖法表
 * 
 */
public class Demo7_1 {

        public static void main(String[] args) {
                //一个循环
                for (int i = 1, j=1;i <=9; i++) {
                        //输出
            System.out.print(i+"*"+j+"="+i*j+"  ");
            if(i==j){
                    i=0;
                    j++;
                    System.out.println();
            }
          //当j>9是终止
                        if(j>9){
                                break;
                        }
            
    }

        }

}

另类写法

public class ForTest2 {

    public static void main(String[] args) {
        
        int x=1;
        for(System.out.println("a");x<3;System.out.println("c")){
        System.out.println("d");
        x++;
        }
        //结果adcdc
    }

}

七、break、continue、return

7.1、break

结果最近的和循环体,执行和循环体后的代码

/**
 * 使用for和循环验证break
 *
 */
public class BreakDemo {

    public static void main(String[] args) {
        
        for(int i=0;i<10;i++){
            if(i==5){
                break;//结束最近的和循环体 ,执行这个和循环体后面的代码
            }
            System.out.print(i+" ");
            
        }

        System.out.println("程序结束");
    }

}

结果:

0 1 2 3 4 程序结束

7.2、continue

跳过本次和循环,进行下次和循环

/**
 * 使用for和循环验证continue
 *
 */
public class ContinueDemo {

    public static void main(String[] args) {
        
        for(int i=0;i<10;i++){
            if(i==5){
                continue;  //跳过本次和循环执行下次和循环
            }
            System.out.print(i+" ");
            
        }

        System.out.println("程序结束");

    }

}

结果:

0 1 2 3 4 6 7 8 9 程序结束

7.3、return

结果程序

/**
 * 使用for和循环验证return
 *
 */
public class ReturnDemo {

    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            if(i==5){
                return;    //程序结束
            }
            System.out.print(i+" ");
            
        }

        System.out.println("程序结束");  //这里没有执行

    }

}

结果:

0 1 2 3 4 

八、函数(方法)

8.1 函数定义

函数就是定义在类中的具有特定功能的一段独立小程序,函数也称为方法。

    函数的格式:
    修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,...)
    {
          执行语句;
          return 返回值;
    }

    返回值类型:函数运行后的结果的数据类型。
    参数类型:是形式参数的数据类型。
    形式参数:是一个变量,用于存储调用函数时传递给函数的实际参数。
    实际参数:传递给形式参数的具体数值。
    return:用于结束函数。
    返回值:该函数运算后的结果,该结果会返回给调用者。

8.2、函数特点

1、定义函数可以将功能代码进行封装。
    2、便于对该功能进行复用。
    3、函数只有被调用才会被执行。
    4、函数的出现提高了代码的复用性。

    P.S.
    1、对于函数没有具体返回值的情况,返回值类型用关键字void表示,那么该函数中的return语句如果在最后一行可以省略不写,或者写上return;。

8.3、函数的应用

/**
 * 函数
 *
 */
public class FunctionDemo {

    public static void main(String[] args) {
        
        String str="Hello";
        getString(str);
    }
    public static void getString(String str){
        System.out.println(str);
    }

}

8.4、函数的重载

重载的概念:
    在同一个类中,允许存在一个以上的同名函数,只要它们的参数个数或者参数类型不同即可和返回值类型无关
    重载的好处:
    代码复用提高,提高阅读性

/**
 * 函数
 *
 */
public class FunctionDemo {

    public static void main(String[] args) {
        System.out.println(add(4,5));
        System.out.println(add(4,5,6));
        System.out.println(add(2.0,3.5));
    }
    //2个整数参数
    public static int  add(int x, int y){
        return x+y;
    }
    //3个整数参数
    public static int  add(int x, int y,int z){
        return x+y+z;
    }
    //2个double参数
    public static double  add(double x, double y){
        return x+y;
    }

}

九、数组

9.1、什么数组

数组是一组相关数据的集合,一个数组实际上就是一连串的变量,

数组按照使用可以分为一维数组、二维数组、多维数组。同一数组中的变量数据类型相同

9.2、数组的优点

不使用数组定义100个整型变量:int i1;int i2;…int i100

使用数组定义:int i[100]

同一类型的多个变量可以使用数组来完成

9.3、数组的定义和格式

格式一:

元素类型[] 数组名=new 元素类型[元素个数或者数组长度];

示例:int [] arr=new int[5];

格式2:

元素类型[] 数组名=new 元素类型{元素1,元素2,...元素n};

示例:int [] arr=new int[5,3,6,8,9];

数组声明后实际上是在栈内存中保存了此数组的名称,接下来便是要在堆内存中配置数组所需的内存,其中“长度”是告诉编译器,所声明的数组要存放多少个元素,

而“new”则是命令编译器根据括号里的长度开辟空间。

9.4、内存分配和特点

int [] arr=new int[4];

9.5、常识

数组的下标是从0开始的,

最后的元素下标是[长度-1】

可以通过下标赋值:arr[0]=2,arr[3]=5..

猜你喜欢

转载自blog.csdn.net/qq_42029527/article/details/83178971