Java笔记第三章

第三章流程控制语句

流程是人们生活中不可或缺的一部分,它表示人们每天都在按照一定的流程做事,这其中的步骤是有顺序的。程序设计也需要有流程控制语句来完成要求,根据输入决定程序要进入什么流程,即“做什么”以及“怎么做”等。


从结构化程序设计角度出发,程序有 3 种结构:顺序结构选择结构循环结构

到目前为止,我们所编写的程序都属于顺序结构。但是事物的发展往往不会遵循早就设想好的轨迹进行,因此,所设计的程序还需要能够具有在不同的条件下处理不同问题以及当需要进行一些相同的重复操作时,如何能省时省力地解决问题的能力。

 

1.顺序结构

若是在程序中没有给出特别的执行目标,系统则默认自上而下一行一行地执行该程序,这类程序的结构就称为顺序结构。

·    通常代码从主函数开始逐行向下运行

·    也是一种宏观的代码运行结构

         {                                        {

语句块      ————>      语句一;

 }                                     语句二;

                                    }

public class Text{
    public static void main(String[] args){
        System.out.println("语句一");
        System.out.println("语句二");
    }
}

 

2.选择结构

if语句

       基本:if (判断条件){

                       语句一;

                 }else{

                        语句二;

                 }

       单if:if(判断条件){

                        语句块;

                 }

       嵌套:if(判断条件){

                         语句块;

                 } else if(判断条件){

                          语句块;

                 }

 

          增加分支。同层级运行一条,达到共同终点

import java.util.Scanner;
public class Home10{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter r1's x,y and width,hight:");
//输入点坐标
		double x1=sc.nextDouble(),y1=sc.nextDouble(),w1=sc.nextDouble(),h1=sc.nextDouble();
		System.out.print("Enter r2's x,y and width,hight:");
		double x2=sc.nextDouble(),y2=sc.nextDouble(),w2=sc.nextDouble(),h2=sc.nextDouble();
		
		double kx=Math.abs(x1 - x2);
		double ky=Math.abs(y1 - y2);
//判断位置关系
		if(kx>=(w1 + w2) / 2 || ky>=(h1 + h2) / 2){
			System.out.println("Out!");
		}else if(kx<=(w1 - w2) / 2&& ky<=(h1 - h2) / 2){
			System.out.println("Inside!");
		}else{
			System.out.println("Overlap!");
		}
	}
}

switch语句

                     switch(){

                            case:

                     }

 break;deafult;

import java.util.Scanner;
public class Home07{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter year: ");
		int year = sc.nextInt();
		System.out.print("Enter month: ");
		int month = sc.nextInt();
		System.out.print("Enter the day of the month: ");
		int day = sc.nextInt();
		
		int q=day,m=month,y=year;
		if(month == 1 || month == 2){
			y--;
			m += 12;
		}
		int j=y / 100,k=y % 100;
		
		int theDay =(int) (q + 26 * (m + 1.0) / 10 + k + k / 4.0 + j / 4.0 + 5.0 * j) % 7;
		System.out.print("The day is ");
//switch分支
		switch(theDay){
			case 0:
				System.out.println("Saturday.");
				break;
			case 1:
				System.out.println("Sunday.");
				break;
			case 2:
				System.out.println("Monday.");
				break;
			case 3:
				System.out.println("Tuesday.");
				break;
			case 4:
				System.out.println("Wednesday.");
				break;
			case 5:
				System.out.println("Thursday.");
				break;
			case 6:
				System.out.println("Friday.");
				break;	
		}
	}
}

3.循环结构

主要解决重复性执行的代码

循环的四要素

        Ⅰ循环初始化                                  指的就是循环从什么时候开始执行

        Ⅱ循环继续条件                               什么情况循环继续执行/反过来说,循环何时结束

        Ⅲ循环体                                          就是我们需要被多次循环执行的代码块

        Ⅳ循环的周期,步长,间距                    当前循环和下一次循环之间的变更

已知重复次数——for

未知重复次数——while

for循环

for(循环初始化;循环继续条件;循环周期){

   循环体;

}

public class Home21{
	public static void main(String[] args){
		for(int i=10000;i>1;i--){                        //从1000到1
			int sum=0;
			for(int j=1;j<i;j++){                    //找因数
				if(i % j==0){
					sum=sum + j;            //因数求和
				}
			}
			if(sum==i)
				System.out.println(i);
		}
	}
}

while循环

       循环初始化;

       while(循环继续条件){                                      do{

       循环体;                                                         }

       循环周期;                                                     while()

       }

import java.util.Scanner;
public class Home23{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter: ");
		int num=sc.nextInt();
		
		int n=1;
		while(true){                                //找到二进制最高位
			if(num / n==0){
				break;
			}else{
				n *= 2;
			}
		}
		
		while(true){                               //从二进制最高位向低位输出
			if(num / n !=0){
				num=num - n;
				System.out.print("1");
			}else{
				System.out.print("0");
			}
			n /= 2;
			if(n<1)
				break;
		}	
	}
}
发布了4 篇原创文章 · 获赞 0 · 访问量 52

猜你喜欢

转载自blog.csdn.net/weixin_44576890/article/details/104263119