Java中控制语句知识点总结

Java学习教程地址:(个人推荐)
https://www.sxt.cn/Java_jQuery_in_action/Object_oriented_and_process_oriented.html

1.随机生成整数

public class New12 {
	
	//Math.random()*101,随机生成[0,100]的整数
	public static void main(String[] args) {
		int[] arr = new int[10];
		for(int i=0; i<10; i++) {
			arr[i] = (int) (Math.random()*101);
			System.out.println(arr[i]);
		}
	}
	
}

2.if(单、双、多)选择结构,条件运算符( ? : )

public class New12 {
	
	public static void main(String[] args) {
		int score = (int) (Math.random()*101);
		System.out.println("分数为:"+score);
		if(score>=80) {
			System.out.println("优秀");
		} else if(score>=60) {
			System.out.println("良好");
		} else {
			System.out.println("壮士仍需努力");
		}
		
		//三元条件运算符x?x:x
		System.out.println(score>=10?"不容易啊":"我想笑·哈哈哈~~~");
	}
	
}

3.switch多选择结构

import java.util.Scanner;

public class New12 {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个英文字母:");
		String word = scanner.nextLine();
		switch(word) {
			case "a":
			case "e":
			case "i":
			case "o":
			case "u":
				System.out.println("元音");
				break;
			case "y":
			case "w":
				System.out.println("半元音");
				break;
			default:
				System.out.println("辅音");
		}
	}
	
}

4.while和do…while循环语句

public class New12 {

	public static void main(String[] args) {
		int i = 1, j = 1;
		int sum = 0, sum2 = 0;
		
		//while
		while(i<=100) {
			sum += i;
			i++;
		}
		System.out.println("while方法运算1到100的整数和:"+sum);
		
		//do  while
		do {
			sum2 += j;
			j++;
		}while(j<=100);
		System.out.println("do...while方法运算1到100的整数和:"+sum2);
	}
	
}

5.for循环(逗号运算符、无限循环)

public class New12 {
	
	public static void main(String[] args) {
		
		//逗号运算符
		for(int i=1, j=i+10; i<5; i++, j=i*2) {
            System.out.println("i= "+i+" j= "+j); 
        } 
		
//		//无限循环,相当于while(true)
//		for ( ; ; ) {
//            System.out.println("无限循环");
//      }
		
	}
	
}

6.foreach循环

①for-each增强for循环在遍历数组过程中不能修改数组中某元素的值。
②for-each仅适用于遍历,不涉及有关索引(下标)的操作。

public class New12 {
	
	public static void main(String[] args) {
		
		int[] arr = {1,2,3,4,5,6};
		for(int i : arr) {
			System.out.println(arr[i-1]);
		}
		
	}
	
}

7.break、continue、return

break:强制退出循环

continue:退出本次循环,执行下次循环

//输出100~150之间不能被3整除的数,并且每行输出5个数
public class New12 {
	
	public static void main(String[] args) {
		
		int count = 0;
		for(int i=100; i<=150; i++) {
			if(i%3==0) {
				continue;
			}
			count++;
			System.out.print(i+"\t");
			if(count%5==0) {
				System.out.println();
			}
		}
	
	}
	
}

return:①结束程序运行、②返回值

8.带标签的break、continue:控制嵌套循环跳转

//打印101到150之间的所有的质数
public class New12 {
	
	public static void main(String[] args) {
		
		outer:for(int i=101; i<=150; i++) {
			for(int j=2; j<i/2; j++) {	//只需要判断到i/2
				if(i%j==0) {
					continue outer;
				}
			}
			System.out.println(i);
		}
	
	}
	
}

9.递归:自己调用自己

①定义递归头:什么时候不调用自己,即递归的结束条件
②定义递归体:什么时候调用自己

import java.util.Scanner;

//计算n的阶乘
public class New12 {
	
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("输入一个整数:");
		int n = scanner.nextInt();
		System.out.println(a(n));
	
	}
	public static int a(int n) {
		if(n==1) {	//递归头
			return 1;
		} else {	//递归体
			return n*a(n-1);
		}
	}
	
}

10.面向对象(封装、继承、多态)

new
this
super
static
package
import
extends
instanceof:是二元运算符,左边是对象,右边是类,用于判断左边对象是否是由右边类创建的,返回值是true或false。不过在eclipse里面应该没有返回false的机会,如果是false,eclipse会直接报错,无法正常运行。

class Father1{
	
}

class Child extends Father1{
	
}

public class New12 {
	
	public static void main(String[] args) {
		Child child = new Child();	
		//由于child不是New12类创建的对象,所以下面一行代码会直接报错
//		System.out.println("child是否是New12类所创建的对象:"+(child instanceof New12));
		
		//子类创建的对象同样相当于父类创建的对象
		System.out.println("child是否是Child类所创建的对象:"+(child instanceof Child));//true
		System.out.println("child是否是Father1类所创建的对象:"+(child instanceof Father1));//true
		
		Father1 father = new Father1();
		//父类创建的对象不等于子类创建的对象
		System.out.println("father是否是Father1类所创建的对象:"+(father instanceof Father1));//true
		System.out.println("father是否是Child类所创建的对象:"+(father instanceof Child));//false
	}
	
}

封装:public、private、protected、缺省
final
abstract
interface
implements

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值