if-else 、while、 do-while 、for、 return、 break、 continue、 foreach、 switch
if-else
public class IfElse {static int result = 0;static int test(int a,int b){if (a<b){result = 1;}else if (a>b){result = -1;}else {result = 0;}return result;}public static void main(String[] args) {System.out.println(test(5,2));}}
public class IfElse1 {static int test(int a, int b){if (a<b){return 1;}else if (a>b){return -1;}else {return 0;}}public static void main(String[] args) {System.out.println(test(5,5));}}
public class IfElse2 {static int result = 0;static void test(int a ,int b){if (a<b){result = 1;}else if (a>b){result = -1;}else {result = 0;}}public static void main(String[] args) {test(7,9);System.out.println(result);}}
while
先判断后循环,判断为false跳出循环
public class While {static boolean test() {//Math.random 随机生成一个大于0小于1的随机数boolean result = Math.random() < 0.5; //将result通过<0.5,定义成Boolean类型,System.out.print(result + ","); //将结果打印出来,便于理解true --while循环继续return result;}public static void main(String[] args) {while (test()) {System.out.println("我在while循环中");}System.out.println("我退出了while循环");}}
do-while
先循环或判断,至少会执行一次,即使表达式第一次就被计算为false
public class DoWhile {static boolean condition(){boolean rand = Math.random()<0.9;System.out.print(rand);return rand;}public static void main(String[] args) {do {System.out.println(" 至少执行一次");}while (condition());}}
public class DoWhileAndWhile {public static void main(String[] args) {//do whileint a=2;do {System.out.println("你好");a++;}while(a<4);}}//结果有两次“你好”class test{//whilepublic static void main(String[] args) {int a =0;while (a<2){System.out.println(a);a++;}}}//结果是0和1
for循环
执行顺序
1,初始化表达式执行,仅执行一次
2,执行boolean表达式进行判断,如果结果为true,执行第3步,如果结果为false,退出循环
3,执行循环体
4,执行步进表达式
5,执行第2步,循环往复……
for (int i = 0; i < 5; i++) {
System.out.println(“for循环体 “ + i); //循环体
}
foreach语法
java中一种更加简洁的for语法用于数组和容器,表示不必创建int变量而去遍历数组
public class ForEachFloat {public static void main(String[] args) {Random rand = new Random(47);float[] f = new float[10];//对于数组来说,foreach编译之后就是or循环for (int i =0;i<10;i++)f[i] = rand.nextFloat(); //nextFloat生成序列中下一个随机的float类型数,介于0.0到1.0for (float x:f) //定义一个float类型的x,并且将f中每个元素赋值给xSystem.out.println(x);}}
String类中有一个方法toCharArray(),它会返回一个char()数组,因此可以用来遍历字符串
public class ForEachString {public static void main(String[] args) {String str = "love me like you do";char[] charArray = str.toCharArray();// for (char x :charArray){// System.out.print(x+" ");// }int len = charArray.length;for (int i =0;i<len;i++){System.out.print(charArray[i]+" ");}}}
return
方法中并不是必须要有return语句,但是一个方法必须要有返回值(viod方法中没有return语句,它有一个隐式的return)
return关键字的用法可以去看上文中ifelse的例子
break和continue
break用于强行退出循环,不执行循环中剩余的语句
continue则停止当前执行的迭代,然后退回循环开始处,开始下一次迭代
public class Break {public static void main(String[] args) {int[] arrayOfInts = {32, 87, 3, 589, 12, 1076, 12,2000, 8, 622, 127};int searchFor = 12;boolean flag = false;int i = 0; //定义索引for ( i =0; i<arrayOfInts.length; i++){if (arrayOfInts[i] == searchFor){flag = true;break;}}if (flag){print("在索引为"+i+"处找到了"+searchFor);}else {print("未找到"+searchFor);}}}
public class Continue {//用continue方法统计字符串中的某个字符个数public static void main(String[] args) {String searchMe = "peter piper picked a peck of pickled peppers";int length =searchMe.length();int numP = 0;boolean flag = false;for (int i = 0; i<length; i++){if (searchMe.charAt(i)=='p'){ //charAt是返回指定索引处的char值,第一个char从索引0处开始flag = true;numP++;continue; //这里可加可不加}}if (flag){System.out.println(numP);}else {System.out.println("数组中未找到");}}
switch
public class Demos {public static void main(String[] agrs) {int a = 9;switch (a) {case 1:System.out.println("1!");case 2:System.out.println("2!");case 3:System.out.println("3!");default:System.out.println("Default!");}}}输出:Default!
default语句如果在所有case后面使用,则可以不用break语句
default语句如果在部分case之前,或在所有case之前,则建议在其语句中加如break语句。
否则,执行完default语句后,会从上往下顺序执行case语句,直到遇到break语句,
如果一直遇不到break语句,则执行完default下方所有的case语句
public Shape next(){switch (rand.nextInt(3)){default:case 0: return new Circle();case 1: return new Square();case 2: return new Triangle();/* default:System.out.println("默认");return null;*/}}
