第一部分:if判断语句
程序分三种:
顺序、分支、循环结构
分支(选择):如果
当我们程序需要选择性(需要判断)执行的时候使用分支
语法:
if(条件判断){
//满足条件后的代码
}else{
//否则执行
}
<60 :打
>=60&&<80: 买糖吃
>=80&&<100: 买游戏机
>=100&&<120: 送女朋友
public class IfDemo{
public static void main(String [] args){
//声明一个变量存储分数
int count = -12; //分数
//程序进行判断如果分数小于60分,被打一顿
if(count>0&&count<60){
System.out.println("男女混合双打....");
}else if(count>=60&&count<80){
System.out.println("买糖吃...");
}else if(count>=80&&count<100){
System.out.println("买游戏机...");
}else if(count>=100&&count<120){
System.out.println("送女朋友...");
}else{
System.out.println("分数统计有误");
}
}
}
==========================分割线================================
第二部分:if的嵌套式判断
嵌套的方式
比如:
运动会 >=6 进入决赛 再以性别分为男子组 和 女子组
import java.util.Scanner;
public class IfElseDemo{
public static void main(String [] args){
//分数 -->找外援 创建一个Scanner对象
Scanner input = new Scanner (System.in);
//屏幕提示
System.out.println("请输入你的比赛成绩:");
//类型接受接收输入台上的值
int score = input.nextInt();
//判断比赛的成绩是否大于等于6
if(score >=6){
//当成绩满足可以进入决赛的要求时,判断性别
System.out.println("请输入你的性别:");
//类型接受接收输入台上的值
char gender = input.next().charAt(0); //取用户输入的字符串中下标为0的数(下标为0,第一个数)
//判断输入的性别
if(gender=='男'){
System.out.println("男子组...");
}else if(gender=='女'){
System.out.println("女子组...");
}else{
System.out.println("性别识别错误");
}
}else{
System.out.println("抱歉,你已被淘汰...");
}
}
}
==========================分割线================================
第三部分:等值判断
Switch:
等值判断
break:
跳过本次判断结构,如果没有他,编译出问题
import java.util.Scanner;
public class SwitchDemo
{
public static void main(String [] args){
//创建一个Scanner
Scanner input = new Scanner (System.in);
//屏幕提示
System.out.println("今天健身第几天哇~");
int day = input.nextInt();
//使用判断语句进行判断
switch(day){
case 1:
System.out.println("黄瓜...");
break;
case 2:
System.out.println("鸡胸肉...");
break;
case 3:
System.out.println("鸡蛋...");
break;
case 7:
System.out.println("你有没有乖乖做训练啊?(1、有/2、没有)");
int result = input.nextInt();
if(result==1){
System.out.println("鸡腿..");
}else{
System.out.println("喝凉水吧~");
}
}
}
}
==========================分割线================================
第四部分:先判断,再执行
while(先判断,再执行)
当同样的行为不停的重复或重复很多遍时用到循环
循环结构由四个部分组成的:
【1】初始化
【2】条件判断
【3】循环体
【4】迭代(更新)
public class WhileDemo
{
public static void main(String [] args){
//[1]初始化 声明一个变量记录将要循环的次数
int i = 1; //从1开始记录
//[2]条件判断
while(i<=100){ //次数10
//[3]循环体 要循环的代码
System.out.println("好好学习,天天向上..."+i);
//[4]迭代
i++; //更新记录循环多少次。不满足条件后停止循环
}
}
}
==========================分割线================================
第五部分:先执行,再判断
Do-while(先执行,在判断)
书写流程:
【1】初始化
【2】循环体
【3】迭代
【4】条件判断
public class DoWhileDemo{
public static void main(String [] args){
//[1]初始化
int i = 1;
do{
System.out.println("小吕哇~"); //【2】循环体
//[3]迭代
i++;
}while(i<=10); //【4】条件判断
}
}
==========================分割线================================
第六部分:for循环
for循环
public class ForDemo{
public static void main(String [] args){
//[1]初始化 声明一个变量记录将要循环的次数
int i = 1; //从1开始记录
//[2]条件判断
while(i<=100){ //次数10
//[3]循环体 要循环的代码
System.out.println("好好学习,天天向上..."+i);
//[4]迭代
i++; //更新记录循环多少次。不满足条件后停止循环
}
//for循环的方式比while循环更简洁,可以将while循环中的四行代码用两行写完
for(int i = 1;i<=10;i++){ //初始化;条件判断;迭代
System.out.println("小仙女真好看~~~~");
}
}
}
==========================分割线================================
第六部分:for循环的嵌套
for循环的嵌套
外循环走一遍 内循环走一轮
外循环:
控制行数
内循环:
列数
public class ForDemo02{
public static void main(String [] args){
int rows = 5;
for(int i =1;i<=rows;i++){ //行数
for(int j = 1;j<=i;j++){ //列数
System.out.print("*");
}
System.out.println(); //换行
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3271/viewspace-2824319/ ,如需转载,请注明出处,否则将追究法律责任。