01、控制语句:
package GUIJIN_kongzhiyuju; import java.util.Scanner; /** * 程序分为三种: * 顺序、分支、循环结构 * 分支(选择):如果 * 当我们程序需要选择性执行性(需要判断)执行的时候使用分支 * * if(条件判断){ * //满足条件后的代码 * }else{ * //否则执行 * } * 列如: * <60 :被打 * >=60&&<80 :买糖吃 * >=80&&<100 :买游戏机 * >=100&&<120:送女朋友 * @author 韦桂金 * */ public class kongzhiDemo01 { public static void main(String[] args) { // 声明一个变量存储分数 int conut = 60; // 分数 Scanner input = new Scanner(System.in); // 程序进行判断如果分数小于60分,被打一顿 if(conut < 60) { System.out.println("被打....."); }else { System.out.println("买糖吃...."); } System.out.println("请输入分数:"); int conut1 = input.nextInt(); if(conut1 < 60){ System.out.println("被打"); }else if(conut1 >=60&& conut1 < 80){ System.out.println("买糖吃..."); }else if(conut1>=80&&conut<100){ System.out.println("买游戏机..."); }else if(conut>=100&&conut<120) { System.out.println("送女朋友..."); }else { System.out.println("分数统计有误"); } } }
package GUIJIN_kongzhiyuju; import java.util.Scanner; /** * 嵌套的方式: * 比如: * 运动会 >=6 进入决赛 在以性别分为男子组和女子组 * @author 韦桂金 * */ public class qiantao_Demo02 { 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的数 // 判断输入的性别 if(gender == ‘男‘) { System.out.println("男子组..."); }else if(gender == ‘女‘) { System.out.println("女子组..."); }else { System.out.println("性别识别错误"); } }else { System.out.println("抱歉,你已被淘汰.."); } } }
package GUIJIN_kongzhiyuju; /** * Switch: * 等值判断 * break: * 跳过本次判断结构,如果没有他,编译出问题 * @author 韦桂金 * */ import java.util.Scanner; public class SwitchDemo03 { 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("喝凉水吧..."); } } } }
循环结构:
package GUIJIN_xunhuanjiegou; /** * while() * 当同样的行为不停的重复或重复很多遍时用到循环 * 循环结构由四个部分组成的: * 【1】初始化 * 【2】条件判断 * 【3】循环体 * 【4】迭代(更新) * @author 韦桂金 * */ public class whileDemo01 { public static void main(String[] args) { // 初始化,声明一个变量记录将要循环的次数 int i = 1; // 从1开始记录 // 【2】条件判断 while(i <= 1000000) // 次数10 { // 【3】循环体 System.out.println("好好学习,天天向上..."+i); // 迭代 i++; // 更新记录循环多次。满足条件后停止循环 } } }
package GUIJIN_xunhuanjiegou; /** * do - while(先执行,在判断) * * 书写流程: * 【1】初始化 * 【2】循环体 * 【3】迭代 * 【4】条件判断 * @author 韦桂金 * */ public class do_whileDemo02 { public static void main(String[] args) { // 【1】初始化 int i = 1; do { System.out.println("快放假了"+i); // 【2】循环体 // 【3】迭代 i++; }while(i <= 10); // 条件判断 } }
package GUIJIN_xunhuanjiegou; /** * for: * * @author 韦桂金 * */ public class forDemo03 { public static void main(String[] args) { // for循环的方式比while循环更简洁,可以将while循环中的四行带代码用两行写完 for(int i = 0;i < 12;i++) { // 初始化;条件判断;迭代 System.out.println("你好!"+i); } } }
package GUIJIN_xunhuanjiegou; /** * for的嵌套: * 外循环走一遍,内循环走一轮 * 外循环: * 控制行数 * 内循环: * 控制列数 * @author 韦桂金 * */ public class ForDemo04 { public static void main(String[] args) { int rows = 5; for(int i = 0; i < rows;i++) { // 行数 for(int j = 0;j <= i;j++) { // 列数 System.out.print("*"); } System.out.println(); // 换行 } } }
02break和方法:
package GUIJIN_Breakyufanfa; /** * BreakAndContinue: * 作用: * break:终止整个循环,程序跳转到循环块外的下一条语句 * Continue:跳过本次循环中剩余语句而执行下一次循环 * * @author 韦桂金 * */ public class breakDemo01 { public static void main(String[] args) { // break:跑十圈 for(int i = 1; i <= 10;i++) { if(i == 5) { System.out.println("现在是第"+i+"圈了,不用跑了"); break; } System.out.println("现在是第"+i+"圈了"); } System.out.println("====================分隔线======================"); // continue:跑十圈 跑五圈的时候, for(int i = 1; i <= 10; i++) { if(i == 5) { System.out.println("现在是第"+i+"圈了,喝口水..."); continue; } } } }
package GUIJIN_Breakyufanfa; /** * 方法: * 方法是定义一个类中的一个小程序,用于完成独立的功能 * * 定义方式: * public static 返回值类型 方法名(数据类型 参数1,数据类型 参数2.....){ * 方法的主体 * 程序语句 * [return 表达式;] * } * * 方法的定义两个明确: * 【1】明确方法运行之后是否有结果,如果有,写结果类型,如果没有 void * 【2】方法在运行的时候,是否有不确定因素,不确定因素作为方法的形式参数 * * 方法的调用: * 【1】方法的调用处负责提供方法的实际参数 * 【2】负责对方法的运算结果进行处理 * * 注意事项: * 【1】方法与方法之间是平级的关系,不能嵌套 * 【2】方法的运算结果叫作返回值,返回值的结果只有一个 * 【3】方法的实际参数与方法的形式参数的类型,个数,顺序完全一致 * @author 韦桂金 * */ public class fangfaDemo02 { // 主方法 程序的入口 public static void main(String[] args) { // 静态方法的使用方式: 类名.静态方法名(). int sum = fangfaDemo02.reduce(21, 45); // 方法调用处,实际参数 System.out.println(sum); // 调用 add(100,32); } // 自定义的方法 (完成两个整数的相减) public static int reduce(int a,int b) { // 形参 int result = a - b; // 减法运算 return result; // 结束方法,并把结果给方法的调用处 } public static void add(int x,int y) { System.out.println(x+y); } }