idea的安装与使用
project-多个模块(小方块)- 建包 -(管理我的类,方便管理阅读)用域名的方式倒过来写 - 类
idea 快捷键的讲解
psvm : 主方法
ctrl + / : 单行注释
ctrl + shift + / : 多行注释
sout : 打印
ctrl + y : 删除一行
ctrl + alt + l :格式化代码(美化)
shift + f6 : 重命名
ctrl + shift + u :大小写转换
alt + 鼠标左键 : 多行输入
变量.iter : 数组的foreach遍历
变量名.fori : for遍历
条件循环语句
选择结构 if
1, if
//if的定义 public class ifDemo { //主方法 public static void main(String[] args) { double score = 100; if(score==100){ System.out.println("奖励一部手机"); } } }
2, if....else
public class ifElseDemo { public static void main(String[] args) { double d = 94; if(d==100){ System.out.println("奖励一部手机"); }else{ System.out.println("打屁股"); } } }//打屁股
3, if.....else if 功能类似于 三元运算符
public class ifElseIfDemo { public static void main(String[] args) { double d = 97; if(d==100){ System.out.println("奖励一部手机"); }else if(d>=90){ System.out.println("奖励一个篮球"); } } } //奖励一个篮球
4, if.....else if....else
public class ifElseIfDemo { public static void main(String[] args) { double d = 87; if(d==100){ System.out.println("奖励一部手机"); }else if(d>=90){ System.out.println("奖励一个篮球"); }else{ System.out.println("打一巴掌"); } } } //打一巴掌
switch
1,需求:int week = 1;1 - 打印星期一 2 - 打印星期二 3 - 打印星期三
public class SwitchDemo { public static void main(String[] args) { int week = 5; switch (week){ 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; default: System.out.println("错误"); } } }
while循环
public class WhileDemo { public static void main(String[] args) { int i = 0; while (i<10){ System.out.println(i); i++; } } }
do while 语句
/* * 需求:定义一个变量,当变量小于16的时候,打印变量值,并且自增 * */ public class WhileDemo { public static void main(String[] args) { int i = 2; do { System.out.println(i); i++; } while (i < 16); } }
for 循环
/* 需求:对 1 到 5 进行求和 * */ public class forDemo { public static void main(String[] args) { int sum = 0; for (int i = 1; i < 6; i++) { sum += i; } System.out.println(sum); } } //15
break continue
作用:跳转语句
1,break 停止循环
/* 需求:对 1 到 5 进行求和 当i=3的时候停止 * */ public class forDemo { public static void main(String[] args) { int sum = 0; for (int i = 1; i < 6; i++) { sum += i; if(i==3){ break; } } System.out.println(sum); } } // 6
2, continue
作用:停止本次循环,继续执行下一次
/* 需求:对 1 到 5 进行求和 当i=3的时候不参与运算 * */ public class continueDemo { public static void main(String[] args) { int sum = 0; for (int i = 1; i < 6; i++) { if(i==3){ continue; } sum += i; } System.out.println(sum); } }
嵌套循环
for(表达式1;表达式2;表达式3){ for(表达式1;表达式2;表达式3){ } } //用 * 打印一个直角三角形 /* 用 * 打印一个直角三角形 * ** *** **** ***** ****** */ public class ForForDemo { public static void main(String[] args) { for(int i = 1;i<7;i++){ for(int j=0;j<i;j++){ System.out.print("*"); } System.out.println(); } } }
break continue return
//break public class ForForDemo { public static void main(String[] args) { for(int i = 1;i<6;i++){ for(int j=0;j<5;j++){ if(i==3){ break; } System.out.print("*"); } System.out.println("a"); } } } *****a *****a a *****a *****a //continue
数组
int[] ages = {20, 21, 23, 18, 16, 23, 26, 29}; int[] ages1 = new int[]{20, 21, 23, 18, 16, 23, 26, 29}; length : 可以查看数组的长度 索引:数组里的下标从0开始 例如: 0 1 2 3 4 5 int[i] = x; 可以用索引覆盖原有数据
数组的遍历
/* * 统计本班学生年龄之和,求年龄的最大值,最小值, 平均值 * */ public class ArraySumDemo { public static void main(String[] args) { int[] ages = new int[]{20, 21, 23, 18, 16, 23, 26, 29}; int sum = 0; int max = 0; for (int i = 0; i < ages.length; i++) { if (max < ages[i]) { max = ages[i]; } sum += ages[i]; } System.out.println(sum); //176 System.out.println(sum / ages.length); //22 System.out.println(max); //29 } }