-
- 1.1. if单选择结构
- 1.2. if双选择结构
- 1.3. if多选择结构
- 1.4. if嵌套结构
- 1.5. switch多选择结构
- 1.6. 循环结构
- 1.6.1. while循环
- 1.6.2. do…while循环
- 1.6.3. !!!for循环结构!!!
- 1.6.4. 增强for循环
- 1.6.5. break , continue
-
- 2.1. 顺序结构
- 2.2. if单选择结构
- 2.3. if双选择结构
- 2.4. if多选择结构
- 2.5. switch多选择结构
- 2.6. while循环结构
- 2.7. do while循环结构
- 2.8. for循环结构
- 2.9. 增强for循环
- 2.10. break , continue
- 2.11. 练习
----基本结构----
1. 基本语法
1.1. if单选择结构
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
1.2. if双选择结构
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为falee
}
1.3. if多选择结构
if(布尔表达式1){
//如果布尔表达式1的值为true
}else if(布尔表达式2){
如果布尔表达式2的值为true
}else(布尔表达式3){
如果以上布尔表达式的值都不为true
}
1.4. if嵌套结构
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}
}
1.5. switch多选择结构
switch(expression){
case value :
//语句
break://可选
case value :
//语句
break://可选
//你可以有任意数量的case语句
default : //可选
//语句
}
switch语句中的变量类型可以是:
- byte、short、int或者char。
- 从Java SE 7开始switch支持字符串String类型了。
- 同时case标签必须为字符串常量或字面量。
1.6. 循环结构
- while循环
- do…while循环
- for循环
- 在java5中引入主要用于数组的增强型for循环
1.6.1. while循环
while(布尔表达式){
//循环内容
}
- 布尔表达式为true,则循环被执行;
- 一般需要一个让表达式失效的方式结束循环;
- 少部分情况需要循环一直执行,比如服务器的请求响应监听等;
- 避免表达式一直为true【死循环】;
1.6.2. do…while循环
至少会循环一次的循环,是先执行后判断
do{
//代码语句
}while(布尔表达式);
1.6.3. !!!for循环结构!!!
for(初始化;布尔表达式;更新){
//代码语句
}
1.6.4. 增强for循环
主要用于数组或集合
for(声明语句 : 表达式)
{
//代码句子
}
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
1.6.5. break , continue
- break直接跳出;
- continue执行最后一次;
- goto功能在break和continue上,使用时用冒号标识符,如 continue : label
2. 例子
2.1. 顺序结构
//顺序结构,没什么好说的
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
System.out.println(6);
}
}
2.2. if单选择结构
package com.wcl.struct;
import java.util.Scanner;
public class ifDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
//equals判断字符串是否相等
if(s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
2.3. if双选择结构
package com.wcl.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//分数大于60及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
2.4. if多选择结构
package com.wcl.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
//成绩
Scanner scanner = new Scanner(System.in);
System.out.println("判定成绩");
Float score = scanner.nextFloat();
if (score==100f){
System.out.println("恭喜满分");
}else if(score>=90){
System.out.println("优秀");
}else if(score>=80){
System.out.println("良好");
}else if(score>=60){
System.out.println("及格");
}else if(score<60 && score>=0){
System.out.println("及格");
}else{
System.out.println("成绩不合法");
}
scanner.close();
}
}
/*
if语句至多有1个else语句,else语句在所有的else if语句之后。
if语句可以有若干个else if语句,它们必须在else语句之前。
一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行。
*/
2.5. switch多选择结构
2.5.1. demo1
package com.wcl.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//switch 匹配一个具体的值
char grade = 'C';
//case穿透现象
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
//break;
case 'C':
System.out.println("及格");
//break;
case 'D':
System.out.println("再接再厉");
//break;
case 'E':
System.out.println("挂科");
//break;
}
}
}
2.5.2. demo2
package com.wcl.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "wcl";
//jdk7以后支持
//字符的本质还是数字
//反编译 Java---class(字节码文件)----反编译(idea)
switch (name){
case "w" :
System.out.println("W");
break;
case "C" :
System.out.println("C");
break;
default:
System.out.println("wcl");
break;
}
}
}
2.6. while循环结构
package com.wcl.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
2.7. do while循环结构
package com.wcl.struct;
public class DoWhileDemo {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
sum = sum + i;
i++;
}while(i<=100);
System.out.println(sum);
}
}
2.8. for循环结构
package com.wcl.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 0;//初始化条件
while (a<=100)//条件判断
{
a++;//迭代
System.out.println(a);//循环体
}
System.out.println("while循环结束!");
//======================
//快捷:100.for 回车
//初始化值/条件判断/迭代
for (int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束!");
/*
关于for循环有以下几点说明:
最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程。
*/
//死循环
for(; ;);{
}
}
}
九九乘法
package com.wcl.struct;
//99乘法表
public class ForDemo02 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((i+"*"+j+"=")+(i*j)+"\t");
if (j==i){
System.out.println();
// System.out.print("\n");
}
}
}
}
}
2.9. 增强for循环
package com.wcl.struct;
//数组
public class ForDemo03 {
public static void main(String[] args) {
int[ ] numbers = {10,20,30,40,50};//定义了一个数组
//遍历数组元素
for (int x:numbers){
System.out.println(x);
}
//=======================================
System.out.println("===========================");
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
}
}
2.10. break , continue
package com.wcl.struct;
public class LabelDemo01 {
public static void main(String[] args) {
//打印101-150之间的质数
int count = 0;
outer:for (int i = 101;i<150;i++){
for (int j = 2;j<i/2;j++){
if (i%j==0){
continue outer;
}
}
System.out.print(i+"\t");
}
}
}
2.11. 练习
package com.wcl.struct;
public class TestDemo01 {
//打印三角形 5行
/*
1 *
3 ***
4 *****
5 *******
6 *********
*/
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}