所有的程序都是按顺序执行
if(a>0){System.out.println(“hello”);}
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字");
score = scanner.nextInt();
if (score>40){
System.out.println("这个大于40");
}
scanner.close();
}
}
if(a>0){System.out.println(“hello”);}
else{System.out.println(“no”);};
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字");
score = scanner.nextInt();
if (score>40){
System.out.println("这个大于40");
}else {
System.out.println("这个小于40");
}
scanner.close();
}
}
if(a>0){System.out.println(“hello”);}
else if(a>1){System.out.println(“no1”);}
else if(a>2){System.out.println(“no2”);}
else{System.out.println(“no3”);};
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01_text {
public static void main(String[] args) {
int score = 0;//初始化成绩
Scanner scanner =new Scanner(System.in); //引入Scanner类
while (scanner.hasNextInt()){ //如果输入是整数就一直运行,否则跳出循环
score = scanner.nextInt(); //打开输入并赋值
if (score>=90){
System.out.println("A级");
}else if(score<90 && score>=80){
System.out.println("B级");
}else if (score<80 && score>=70){
System.out.println("C级");
}else if(score<70 && score>=60){
System.out.println("D级");
}else if (score<60 && score>=0){
System.out.println("成绩不合格");
}else {
System.out.println("输入成绩不合法");
}}
scanner.close();//关闭输入
}
}
判断一个变量与一系列值中某个值是否相等,每个值成为一个分支。
switch(expression表达式){
case value常量:{};break; //可选
case value常量:{};break;//可选
default:{}//可选
package com.ckw.blog.select;
import java.util.Scanner;
public class demo02 {
public static void main(String[] args) {
String str = "K";
Scanner scanner = new Scanner(System.in);
str = scanner.next();
switch (str){
case "A"://匹配字符
System.out.println("成绩优秀");
break;//如果不加会出现case穿透
case "B":
System.out.println("成绩中等");
break;
case "D":
System.out.println("成绩一般");
break;
case "E":
System.out.println("成绩偏差");
break;
case "F":
System.out.println("成绩不及格");
break;
default:
System.out.println("未知输入");
}
}
}
if 用于比较数字,作为选择
switch 主要用于匹配多种字符结果,做出选择
(源码还是用if作为编译)
package com.ckw.blog.select;
import java.util.Scanner;
public class study01 {
public static void main(String[] args) {
int score = 0;//初始化成绩
String str = "k";//初始化等级
Scanner scanner =new Scanner(System.in); //引入Scanner类
while (scanner.hasNextInt()){//如果输入是整数就一直运行,否则跳出循环
score = scanner.nextInt(); //打开输入并赋值
if (score>=90&&score<=100){
str = "A";
}else if(score<90 && score>=80){
str = "B";
}else if (score<80 && score>=70){
str = "C";
}else if(score<70 && score>=60){
str = "D";
}else if (score<60 && score>=0){
str = "E";
}else {
str = "F";;
}
switch (str){
case "A"://匹配字符
System.out.println("成绩优秀");
break;//如果不加会出现case穿透
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("未知输入");
}}
scanner.close();//关闭输入
}
}
第一种if结构。
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int score = 0;
int nub = 40;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字");
while (score!=nub){
score = scanner.nextInt();
if(score>nub){
System.out.println("大了");
}else if(score<nub){
System.out.println("小了");
}
}
System.out.println("恭喜你答对了");
scanner.close();
}
}
package com.ckw.blog.select;
public class while001 {
public static void main(String[] args) {
int a = 0;
while(a<100){System.out.println(a)};
}
}
先执行一次再进入循环
package com.ckw.blog.select;
public class while001 {
public static void main(String[] args) {
int a = 0;
do{a++;System.out.println(a);}while(a>=1&&a<100);
//先执行一次代码,如果条件为turn,则再进入循环
}
}
while是先判断后执行,do…while是先执行后判断
do…while运行至少会执行一次循环
package com.ckw.blog.select;
public class while001 {
public static void main(String[] args) {
int a = 0;
while (a>0&&a<100){
System.out.println("我是结果1的");
System.out.println(a);
a++;
}
do{a++;
System.out.println("我是结果2的");
System.out.println(a);}while (a>0&&a<100);
//先执行一次代码,如果条件为turn,则再进入循环
}
}