#流程结构语句
定义:通过一些语句,来控制程序的执行流程
顺序结构语句:没有特定语法结构,按照代码先后顺序,依次执行
if语句
if语句格式1
public class Demo1{
public static void main (String [] args){
//如果年龄大于18岁就可以上网吧
int age = 17
if (age >=18){
System .out.println("可以上网吧");
}
}
}
注意事项
- 如果if控制的语句是一条语句,大括号可以不写,但不建议省略
- if语句小括号后面不写分号
if语句格式2
if(关系表达式){
语句体1;
}else {
语句体2;
}
首先计算关系表达式的值
如果关系表达式的值为true,就执行语句体1,
如果关系表达式的值为false,就执行语句体2,
继续执行后面的语句
int num = 9;
if (num % 2 == 0){
System.out.println("偶数");
}else {
System.out.println("奇数");
}
if语句格式3
if(判断条件1){
语句体1;
}else if(判断条件2){
语句体2;
}
...
else{
语句体n+1;
}
public class A{
public static void main (String [] args ){
int score=100;
if (score <=100 & score >=90){
System.out.println("youxiu");
}else if(score <90 & score >=80){
System.out.println("lianghao");
}else if(score <80 & score >=70){
System.out.println("yiban");
}else {
System.out.println("youwu");
}
}
小练习
import java.util.Scanner;
//DemoScanner可以改为其他名称
public class DemoScanner{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("qingshuru")
int score=sc.nextInt();
//先判断成绩是否在合法范围内
if (score >= 0 & score <= 100){
if (score >90 & score <=100 ) {
System.out.println("youxiu");
}else if (score >80 & score <=90 ) {
System.out.println("lianghao");
}
}else {
//不在合法范围内,就给出错误提示
System.out.println("you wu");
}
}
}
Switch语句
case 后面的值不可以重复
case 后面的值只能是常量,不能是变量
public class A{
public static void main (String [] args){
int week =5;
switch (week){
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
case 4 :
System.out.println("Thursday");
break;
case 5 :
System.out.println("Friday");
break;
case 6 :
System.out.println("Saturday");
break;
case 7 :
System.out.println("Sunday");
break;
default:
System.out.println("error");
break;
}
}
}
switch case穿透
如果switch语句中,case省略了break语句, 就会开始穿透
public class A{
public static void main (String [] args){
int week =5;
switch (week){
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
System.out.println("工作日");
break;
case 6 :
case 7 :
System.out.println("休息日");
break;
default:
System.out.println("error");
break;
}
}
}
break再理解标注下
循环
for 循环
public class A{
public static void main (String [] args){
for(int i =1; i <= 3; i++){
System.out.println("run")
}
}
}
public class A{
public static void main (String [] args){
int sum = 0
for(int i =1; i <= 1; i++){
sum += i;
}
System.out.println("1-5之间的数据的和是" + sum)
}
}
public class A{
public static void main (String [] args){
int sum = 0
for(int i =1; i <= 100; i++){
if (i % 2 =0 ){
sum+=i
}
}
System.out.println("1-100之间的偶数的和是" + sum)
}
}
找出水仙花数
public class A{
public static void main (String [] args){
int sum = 0;
for(int i =100; i <= 999; i++){
int ge = i%10;
int shi = i /10%10;
int bai = i /10/10%10;
if (ge*ge*ge+shi*shi*shi+bai*bai*bai==i ){
System.out.println(i);
}
}
}
}
public class A{
public static void main (String [] args){
//定义变量count,用于保存“打印过”的数量
int count = 0
int sum = 0;
for(int i =100; i <= 999; i++){
int ge = i%10;
int shi = i /10%10;
int bai = i /10/10%10;
if (ge*ge*ge+shi*shi*shi+bai*bai*bai==i ){
//在判定和打印水仙花数的过程中,拼接空格,并不换行,记录数量
System.out.print(i+"");
count++;
if (count % 2 ==0){
System.out.println();//println里面没写,自动换行
}
}
}
}
}
while循环语句
public class A{
public static void main (String [] args){
int i = 1;
while( i <=100){
System.out.println(i);
i++;
}
}
}
不明确循环次数,用while
明确,用for
public class A{
public static void main (String [] args){
double i = 0.1;
int count = 0;
while( i <=8844430){
i*=2;
count++;//次数自增
}
System.out.println(count);
}
}
do…while 循环 使用非常少
特点
无论条件是否满足,都至少会执行一次
因为从上到下都要执行
三种循环区别
for,while先判断,后执行
do while先执行后判断
for和while的区别
条件语句控制的自增变量,因为归属for循环的语法结构中,在for循环结束后,就不能再访问到了
条件语句控制的自增变量,while循环不归属其语法结构中,在while循环结束后,该变量还可以继续使用
死循环
//for死循环
for(;;){
System.out.println(xxx);
}
**//while死循环,最常用**
while(true){
System.out.println(xxx);
}
//do while死循环
do{System.out.println(xxx);
}while(true);
System.out.println(abc);//此段代码无法执行,因为上面都是死循环
//命令提示符窗口中可按ctrl+c结束死循环
跳转控制循环
public class A{
public static void main (String [] args){
for(int i =1;i<=24;i++){
if(i == 4){
continue;
}
System.out.println(i+"floor");
}
}
}
public class A{
public static void main (String [] args){
for(int i =20;i<=80;i++){
if(i == 60){
break;
}
System.out.println(i+"floor");
}
}
}
continue 跳过某次循环的内容
break 终止某次循环
import java.util.Scanner;
public class A{
public static void main (String [] args){
loop:while ( true){//给while 命名,便于break终止
System.out.println("press number to check");
System.out.println("press 0 to exit");
Scanner sc=new Scanner(System .in);
int week = sc.nextInt();
//多种情况判断,switch语句
switch(week){
case 0:
System.out.println("thank for your using");
break loop;
case 1:
System.out.println("swim");
break;
case 2:
System.out.println("jog");
break;
}
}
}
随机数
import java.util.Random;
public class A{
public static void main (String [] args){
Random r =new Random();
int number =r.nextInt (10)+1; //(0+1,9+1) 取值区间可以调整
System.out.println(number);
}
}
代办:
问王老师要那两门课
哪些位置空格,哪些位置不空格
gis退费
while
if
else
语法