Java005

day6

选择结构

if选择结构

  • 单选双选结构

package priv.youfeng.ProcessControl;
​
import java.util.Scanner;
​
public class studyIf {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("请输入内容:");
        String str=s.nextLine();
​
        //equals:判断字符串是否相等
        if (str.equals("A")){
            System.out.println(str);
        }
        System.out.println("END");
        s.close();
        /*Scanner scan=new Scanner(System.in);
        int score=scan.nextInt();
        if (score>=60){
            System.out.println("恭喜你及格了");
        }else{
            System.out.println("很抱歉你没及格了");
        }*/
    }
}

 


 

  • 多选结构

package priv.youfeng.ProcessControl;
​
import java.util.Scanner;
​
public class ifExpand {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int score=scan.nextInt();
        if (score==100){
            System.out.println("恭喜你满分了");
        }else if (score>=90 && score<100){
            System.out.println("恭喜你成绩达到A级");
        }else if (score>=80 && score<90){
            System.out.println("恭喜你成绩达到B级");
        }else if (score>=70 && score<80){
            System.out.println("恭喜你成绩达到C级");
        }else if (score>=60 && score<70){
            System.out.println("恭喜你成绩达到D级");
        }else if (score<60 && score>=0){
            System.out.println("成绩不及格");
        }else{
            System.out.println("成绩输入错误,请重新输入");
        }
    }
}

 


if语句中至多有一个else语句,else语句在所有的else if语句之后

if语句可以有若干个else if语句,他们必须在else语句之前

一旦其中一个else if语句检测为true,其他的else if都会被跳过执行

switch

switch语句中的变量类型可以是:

  • byte,short,int或者char

  • 从javaSE7开始

  • switch支持字符串String类型了

  • 同时case标签必须为字符串常量或者字面量

package priv.youfeng.ProcessControl;
​
public class studySwitch {
    public static void main(String[] args) {
        //case穿透    //switch 匹配一个具体的值
        char grade='E';
        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;
            default:
                System.out.println("输入有误");
        }
    }
}

 


while循环

它的结构为

while(布尔表达式){
   //循环内容
}
  • 只要布尔表达式为true,循环就会一直执行下去

  • 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环

  • 少部分情况需要循环一直执行,比如服务器的请求响应监听等

  • 循环条件一直为true就会造成【死循环】,正常业务中应避免死循环,会影响程序性能或者造成程序卡死崩溃

package priv.youfeng.ProcessControl;
​
public class studyWhile {
    public static void main(String[] args) {
        int i=0;//输出1-100
        int score=0;
        while (i<100){
            i++;
            score+=i;
            System.out.println(i);
​
        }
        System.out.println(score);
    }
}

 


For循环

  • for循环语句是支持迭代的一种通用技术,是最有效,最灵活的循环结构

  • for循环执行的次数是在执行前就确定的,语法格式如下:

for(初始化;布尔表达式;更新){
   代码语句
}
package priv.youfeng.ProcessControl;

public class ForExpand {
public static void main(String[] args) {
int oddSum=0;
int evenSum=0;

for (int i = 1; i < 100; i++) {
if (i%2!=0){
oddSum+=i;
}else{
evenSum+=i;
}
}
for (int i = 1; i <=1000; i++) {
if (i%5==0){
System.out.print(i+" ");//输出被5整除的数
if (i%15==0){
System.out.println("");//每行输出三个后换行
}
}
}

System.out.println("奇数和"+oddSum+"偶数和"+evenSum);

int sum=0;
for (int i = 1; i <=9; i++) {
for (int i1 = 1; i1 <=i; i1++) {
sum=i*i1;
System.out.print(i1+"*"+i+"="+sum+"\t");}
System.out.println();
}

}
}
package priv.youfeng.ProcessControl;

public class ForExpandTwo {
    public static void main(String[] args) {
        int[] numbers={10,20,30,40,50};//定义了一个数组

        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

 

 

posted @ 2021-12-13 22:49  有枫  阅读(36)  评论(0)    收藏  举报