与任何程序设计语言一样,java使用条件语句和循环结构确定控制流。本文将简单讲解条件、循环和switch。
一、块作用域
块(block),即复合语句。是指由一对大括号括起来的若干条简单的Java语句。块确定了变量的作用域。
比如:
1 public class Code { 2 static 3 { 4 System.out.println("1"); 5 } 6 { 7 System.out.println("2"); 8 } 9 public Code()10 {11 System.err.println("3");12 }13 public static void main(String[]args)14 {15 new Code();16 }17 }注意:不能在嵌套的两个块中声明同名的变量。
立即学习“Java免费学习笔记(深入)”;
二、条件语句
格式1:
if(condition)
{
statement1
statement2
........
}
例如:
1 if(youSales>=target)2 {3 performance="Satisfactory";4 bonus=1000;5 }格式2:
if(condition)statement1 else statement2
例如:
1 if(youSales>=target) 2 { 3 performance=“Satisfactory”; 4 bonus=100+10*(youSales-target“); 5 } 6 else 7 { 8 performance=”Unstatisfactory“; 9 bonus=0;10 }
三、循环
当条件为true时,while循环执行。
格式1:
while(condition)statemnet
例如:
1 while (balance
格式2:
do statement while(condition);
1 do 2 { 3 balance+=payment; 4 double interest=balance*interestRate/100; 5 balance+=interest; 6 7 year++; 8 9 System.out.printf("After year %d,your balance is %,.2f%,year,balance");10 11 System.out.print("Ready to retire?(Y/N)");12 input=in.next();13 }14 while(input.equals("N"));15 16 }四、确定循环
for循环语句是支持迭代的一种通用结构,利用每次迭代之后更新的计数器或类似的变量来控制迭代的次数。
格式类似如下:
for(int i=0;i System.out.println(i); 例子4个: 五、多重选择:switch语句 格式类似如下: switch(choice) { case 1: ........ break; case 2: ....... break; ......... //可以再来几个case(用break结束一下) default: ....... break; } 注意: case标签可以是: * 类型为char、byte、short或int的常量表达式。 * 枚举常量 * 从Java SE 7开始,case标签还可以是字符串字面量。 1 public class ShuZu1 { 2 public static void main(String[]args){ 3 int [][] x={{1,2,2,2,2},{3,3,3,3,3},{4,5,-1,17,55}}; 4 int result=qiuHe(x); 5 System.out.println("和是"+result); 6 } 7 public static int qiuHe(int[][]x){ 8 int s=0; 9 for(int i=0;i 1 public class ShuZu2 { 2 public static void main(String[]args){ 3 int [][] x=new int[7][7]; 4 //生成随机数组,注意没有返回值,另外打印一行字 5 suiJi(x); 6 System.out.println("生成的数组是:"); 7 8 //显示数组内容,注意没有返回值 9 showArray(x);10 11 //取值12 float result=getAvg(x);13 System.out.println("平均数是"+result);14 15 }16 static float getAvg(int [][] x){17 float s=0;18 for(int i=0;i 1 import java.util.Arrays; 2 public class SuZu3{ 3 public static void main(String[] args) { 4 int [] x={2,-1,7,777,6,764,85896,65554,0,874785,417825,74};
5 sort(x,'n'); 6 for(int i=0;i 1 import java.util.Scanner; 2 3 public class Suzu4 { 4 public static void main(String[] args) { 5 System.out.println("请输入");//这个命令只能紧贴在Scanner scan = new Scanner(System.in);的上面或下面才有效。 6 Scanner scan = new Scanner(System.in); 7 //System.out.println("请输入");或者放在Scanner scan = new Scanner(System.in);的下面 8 String str = scan.nextLine();// nextLine才是接收一行 9 10 char[] s = str.toCharArray();// 把字符串转换称一个字符数组11 scan.close();12 int letterCount = 0;13 int numberCount = 0;14 int spaceCount = 0;15 int otherCount = 0;16 for (int i = 0; i < s.length; i++) {17 if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') {18 letterCount++;19 } else if (s[i] >= '1' && s[i] <= '9') {20 numberCount++;21 } else if (s[i] == ' ') {22 spaceCount++;23 } else {24 otherCount++;25 }26 }27 System.out.println("字母有" + letterCount + "个");28 System.out.println("数字有" + numberCount + "个");29 System.out.println("空格有" + spaceCount + "个");30 System.out.println("其他有" + otherCount + "个");31 }32 }//ctrl+shift+f 是代码格式化33 //ctrl+shift+o 是导包











