一、顺序结构:
1、 if循环
- if (关系表达式){
语句块 - }
public class IfDemo {
public static void main(String[] args) {
//顺序结构
System.out.println("1");
System.out.println("2");
System.out.println("3");
//if语句
int age = 19;
if(age < 18) {
System.out.println("未成年");
}
int score = 88;
if(score <= 90){
System.out.println("成绩为良");
}
System.out.println("-----");
}
}
2**、if else循环:**
-
if(关系表达式){
-
语句一
-
}else{
-
语句二
-
}
public class IfElseDemo {
public static void main(String[] args) {
int age = 19;
if (age > 18){
System.out.println(“ojbk”);
}else{
System.out.println(“小屁孩”);
}
}
}
3、IfElseIf循环
import java.util.Scanner;public class IfElseIfDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个数字"); int score = sc.nextInt(); if (score >= 60 && score < 70){ System.out.println("黄金"); }else if(score >= 70 && score < 80){ System.out.println("钻石"); }else if(score >= 80 && score < 90){ System.out.println("星耀"); }else if(score >= 90 && score <= 100){ System.out.println("最强王者"); }else if(score < 60 && score > 0){ System.out.println("黑铁"); }else { System.out.println("输入有误"); } sc.close(); } }
练习
-
猜数字游戏:
-
1、用户藏一个数字 522 number
-
2、其他用户去猜:guess
-
循环的过程guess!=number
-
1)提示用户开始猜
-
2)guess>number 提示:猜大了
-
3)guess<number 提示:猜小了
-
4)guess==number 猜对了
import java.util.Scanner;
public class GuessDemo {
public static void main(String[] args) {int number = (int)(Math.random()*1000);//猜数字 System.out.println("开始猜吧"); //创建对象 Scanner sc = new Scanner(System.in); //接收数据---guess:猜的数字 int guess = sc.nextInt(); while(guess != number){ if(guess>number){ System.out.println("猜大了"); }else{ System.out.println("猜小了"); } guess = sc.nextInt(); } if(guess==number){ System.out.println("猜对了"); } sc.close(); }
}
练习
用ifelse做出猜数字游戏
import java.util.Scanner;public class practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);System.out.println("请输入一个年份"); int year = sc.nextInt(); System.out.println("请输入一个月份"); int month = sc.nextInt(); if (year%4 == 0 && year%100!=0 || year%400 == 0){ System.out.println(" "); }else if(month == 2){ System.out.println("这月有29天"); }else if(month <= 7 & month % 2 == 1 || month >= 8 & month % 2 == 0){ System.out.println("这月有31天"); }else{ System.out.println("这月有30天"); } if (year % 4 != 0){ System.out.println(" "); }else if(month == 2){ System.out.println("这月有28天"); }else if(month <= 7 & month % 2 == 1 || month >= 8 & month % 2 == 0){ System.out.println("这月有31天"); }else{ System.out.println("这月有30天"); } sc.close(); }
}
-
3、switch(变量/表达式){
-
case 字面值1:语句块1;break;
-
case 字面值2:语句块2;break;
-
……
-
default语句块n;
-
}
-
switch结构:
-
表达式的取值
-
byte,short,int,char
-
JDK5,加入了枚举
-
JDK7之后,加入了String
public class SwitchCaseDemo {
public static void main(String[] args) {
int weekday = 2;switch (weekday) { case 1: System.out.println("星期1"); break; case 2: System.out.println("星期2"); break; case 3: System.out.println("星期3"); break; case 4: System.out.println("星期4"); break; case 5: System.out.println("星期5"); break; case 6: System.out.println("星期6"); break; case 7: System.out.println("星期7"); break; } }
}
练习:
输入一个年份,一个月份,输出该月由多少天(用switch)
import java.util.Scanner;
public class lianxi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个年份");
int year = sc.nextInt();
System.out.println("请输入一个月份");
int month = sc.nextInt();
int day = sc.nextInt();
switch (month) {
case 1:
System.out.println("31天");
break;
case 3:
System.out.println("31天");
break;
case 5:
System.out.println("31天");
break;
case 7:
System.out.println("31天");
break;
case 8:
System.out.println("31天");
break;
case 10:
System.out.println("31天");
break;
case 12:
System.out.println("31天");
break;
case 4:
System.out.println("31天");
break;
case 6:
System.out.println("31天");
break;
case 9:
System.out.println("31天");
break;
case 11:
System.out.println("31天");
break;
case 2:
day = year%4 == 0 && year%100!=0 || year%400 == 0 ? 29:28;
break;
}
sc.close();
}
}
二、循环流程控制语句
1、for循环格式及基本使用
-
格式:
-
for(1初始化语句;2判断条件语句;4控制条件语句){
-
3循环体语句;1234 234
-
}
-
for循环:
-
1、三要素:
-
2、执行顺序:
-
3、通常用于指定次数的循环
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i < 3; i++){
System.out.println(i +"");
}
/*
* 第一步:i=0
* 第二步:判断i 是不是小于3、true
* 第三步:输出0
* 第四步:i++
* 重复第二步:判断i是不是小于3、true
* 重复第三步:输出1
* 重复第四步:i++(i=2)
* 重复第二步:判断i是不是小于3、true
* 重复第三步:输出2
* 重复第四步:i++(i=3)
* 重复第二步:判断i是不是小于3
* false
* 退出
*/
int sum = 0;
for(int i = 0; i < 3; i++){
sum += i;
}
System.out.println(sum);}
}
练习:
用for循环数水仙花数
import java.util.Scanner;public class flower {
public static void main(String[] args) {Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); for (int i = 100; i < 1000; i++){ int firstNum = i / 100;//百位 int secondNum = i / 10 % 10;//十位 int thirdNum = i % 10;//个位 if(firstNum*firstNum*firstNum + secondNum*secondNum*secondNum + thirdNum*thirdNum*thirdNum == i){ System.out.println("水仙花数为;"+i); } sc.close(); } }
}
2、 while循环语句: -
1、判断条件
-
2、执行顺序
-
2、while循环格式及基本使用
-
格式:
-
基本格式
-
while(判断条件语句){
-
循环体语句;
-
}
-
扩展格式
-
初始化语句
-
while(判断条件语句){
-
循环体语句;
-
控制条件语句;
-
}
public class WhileDemo { public static void main(String[] args) { String[] player = {"周星驰","刘德华","周慧敏"}; String[] cards = {"黑桃a","黑桃K","黑桃Q","红桃A","红桃J","黑桃a","黑桃K","黑桃Q","红桃A","红桃J"}; //发牌 int index = 0; while (index < cards.length){ String card = cards[index]; String people = player[index++%3]; System.out.println(people + ":" + card + ""); //每发完一次牌,进行换行 if (index % 3 == 0){ System.out.println(); } } } }
DoWhile循环
-
do-while循环
-
区别于while循环,首先执行的是循环体
-
执行完毕后才进行判断Boolean表达式,值为true继续,false退出
-
格式:
-
do{
-
//循环体
-
}while(布尔表达式);
import java.util.Scanner;public class DoWhileDemo { public static void main(String[] args) { int number = 333; System.out.println("开始猜吧"); Scanner sc = new Scanner(System.in); int guess = sc.nextInt(); do{ if(guess>number){ System.out.println("猜大了"); }else{ System.out.println("猜小了"); } guess = sc.nextInt(); if(guess == number){ System.out.println("猜对了"); } }while(guess != number); sc.close(); } }