流程图
1、顺序结构:按照顺序逐行执行
2、循环结构
A、if语句
格式一:
if(关系表达式){
语句块
}else{
语句块2
}
代码应用
package CodeDemo03;
// 复制当前代码行到下一行 atrl+alt+↓
import java.time.Year;
import java.util.Scanner;
public class IfDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System .in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if (age<18) {
System.out.println("你是未成年!");
}else{
System.out.println("你已经成年!");
}
System.out.println();
}
}
格式二:
if(关系表达式){
语句体1;
}else if(关系表达式2){
语句体2;
}
…
else{
语句体n+1;
}
代码应用
package CodeDemo03;
import java.util.Scanner;
public class IfElseIfDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的rank分数:");
int rank = sc.nextInt();
if (rank > 100 || rank < 0) {
System.out.println("你输入的分数无效,请重新输入!");
}else if (rank>=90 && rank <= 100) {
System.out.println("你的段位是最强王者");
}else if (rank>=80 && rank < 90) {
System.out.println("你的段位是星耀");
}else if (rank>=70 && rank < 80) {
System.out.println("你的段位是钻石");
}else if (rank>=60 && rank < 70) {
System.out.println("你的段位是黄金");
}else if (rank<60) {
System.out.println("你的段位是黑铁");
}
}
}
import java.util.Scanner;
public class IfElseIfYearsDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要判断的年份和月份:");
int years = sc.nextInt();
int month = sc.nextInt();
if (month == 2) {
if (years % 4 == 0 || years % 400 == 0 && years % 100 != 0) {
System.out.println(years+"年2月有29天");
}else {
System.out.println(years+"年2月有28天");
}
}else if(month == 1 || month == 3|| month == 5|| month == 7|| month == 8|| month == 10|| month == 12){
System.out.println(years+"年"+month+"月有31天");
}else {
System.out.println(years+"年"+month+"月有30天");
}
}
}
B、switch语句
switch(变量/表达式){
case字面值1:语句块1;break;
case字面值2:语句块2;break;
default:语句块n
}
代码应用
package CodeDemo03;
import java.util.Scanner;
/**
* switch结构
* 表达式取值
* byte,short,int,char
* JDK5加入了枚举
* JDK7加入了String
* @author Administrator
*
*/
public class SwitchDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System .in);
System.out.println("请输入一个数:");
int day = sc.nextInt();
switch (day){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
}
}
}
package CodeDemo03;
import java.util.Scanner;
public class SwitchYearsDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要判断的年份和月份:");
int years = sc.nextInt();
int month = sc.nextInt();
if (years % 4 == 0 || years % 400 == 0 && years % 100 != 0 && month == 2) {
System.out.println(years+",2月有29天");
}else{
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(years+", "+month +"月有31天");
break;
case 2:
System.out.println(years+", "+month +"月有28天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(years+", "+month +"月有31天");
break;
}
}
}
}
C、for语句
for(初始化语句 ;判断条件语句;控制条件语句){
循环体语句
}
package CodeDemo03;
/**
* for循环
* 1、三要素
* 2、执行顺序
* 3、通常用于指定次数的循环
*/
public class ForDemo {
public static void main(String[] args) {
int sum = 5;
for (int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}
/*
* 运行过程
* 1、 i = 0
* 2、判断 i 是否小于 3,true
* 3、输出0,
* 4、若 2 输出为false,则 i++
* 5、重复第二步判断 i 是否小于3
* 6、重复第三步
* 7、重复第四步
* 8、重复第二部判断 i 是否小于3
* 9、重复第三步
* 10、重复第四步
*.......
*直到i != 3,跳出循环输出 i 的结果
*
* */
D、while语句
格式一:
while(判断条件语句){
循环体语句;
}
格式二:
初始化语句;
while(判断条件语句){
循环体语句;
控制条件语句;
}
代码应用
package CodeDemo03;
/**
* while
* 1、判断条件
* 2、执行顺序
* @author Administrator
*
*/
public class WhileDemo {
public static void main(String[] args) {
String[] player = {"郭渣渣","孙牛牛","王菜菜"};
String[] cards = {"黑桃A","黑桃K","红桃Q","方块A","梅花9","黑桃J","红桃8"};
//发牌
int index = 0;
while (index < 6) {
String card = cards[index];
String people = player[index ++% 3];
System.out.println(people+":"+card+" ");
//每发完一次牌,进行换行
if (index % 3 == 0) {
System.out.println();
}
}
}
}
package CodeDemo03;
import java.util.Scanner;
public class WhileGuessDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = (int)(Math.random()*1000);
System.out.println(number);
System.out.println("开始猜吧");
//创建对象
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("猜对了");
}
}
}
do-while格式
无论执行几次,代码都一定会执行一次
do{
循环体语句;
}while(Boolean表达式);
代码应用
Scanner sc= new Scanner(System.in);
int num =(int)((Math.random()*1000)+1);//生成0-1之间的随机数字,再乘以1000就是0-1000之间的随机数
System.out.println("被猜数字:" + num);
System.out.println("请输入你要猜的数字:");
int guess ;
do{
guess = sc.nextInt();
if(guess < num){
System.out.println("猜小了");
}else if(guess > num){
System.out.println("猜大了");
}
System.out.println("继续猜:");
//guess = sc.nextInt();
}while(guess != num);
if(guess == num){
System.out.println("恭喜你猜对了!");
}