先说一下java中的关键字:他们表示一种数据类型或者一种结构,对java编译器来说具有特殊的意义。大体可以分为一下几类
1)访问控制 private protected public
2)类方法属性的修饰 ?abstract class static extend interface implement final native new strictfp synchronized transient volatile?
3)程序控制语句 break continue return do while if else for swich case default ?instanceof
4)错误处理 try catch finally throw throws?
5)包 import package
6)基本类型 boolean byte char short int long float double ?null true false?
7)变量引用 super this void
java保留字
const goto 保留字没有用到但是也不允许用户定义与其冲突的**
下面看流程语句
1、if(boolean){} else if(boolean){}else{}
2、swich(){case ...,case ...,default ...}
注意:1)swich括号内的值必须是 int short byte char 枚举
2)case 后面的值必须不相等,而且是常量
3)如果执行了语句没有执行break 还会执行后面的语句 切忌不要忘了break
4)无论default写在哪个位置他都是最后执行的
3、for循环次数固定
for (int j = 1; j < 10; j++) { System.out.print(j + "*" + i + "=" + i * j + "\t"); if (i == j) break; } System.out.println(); }
?4、while循环次数未知
while(i<10){ int j=1; while (j<10) { System.out.print(j + "*" + i + "=" + i * j + "\t"); if (i == j)break; j++; } i++; System.out.println(); }
?4、do {}while()循环未知 至少循环一次
?
int i = 1; do { int j = 1; do { System.out.print(j + "*" + i + "=" + i * j + "\t"); if (i == j) break; j++; } while (j < 10); System.out.println(); i++; } while (i < 10);?
命令行计算器下例子
?
double[] d = {0.0,0.0}; char operatorString = '+'; while (true) { Scanner scanner = new Scanner(System.in); String string = scanner.next().toLowerCase(); if (string.equals("exit")) { System.exit(0); } else if (string.equals("help")) { System.out.println("小小计算器帮助:只有加减乘除基本运算"); System.out.println("------>1、exit 退出"); System.out.println("------>2、clear 重置"); System.out.println("------>2、= 结果"); } else if (string.equals("=")) { switch (operatorString) { case '+': System.out.println((d[0] + "+" + d[1] + "=") + (d[0] + d[1])); d = null; break; case '-': System.out.println((d[0] + "-" + d[1] + "=") + (d[0] - d[1])); d = null; operatorString = '+'; break; case '*': System.out.println((d[0] + "*" + d[1] + "=") + (d[0] * d[1])); d = null; operatorString = '+'; break; case '/': System.out.println((d[0] + "/" + d[1] + "=") + (d[0] / d[1])); d = null; operatorString = '+'; break; default: System.out.println("运算符无效"); d = null; operatorString = '+'; break; } } else { Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*"); Matcher isNum = pattern.matcher(string); if (isNum.matches()) { double h = Double.parseDouble(string); if(d==null||d[0]==0.0){ d=new double[2]; d[0]=h; } else { d[1]=h; } } else { operatorString=string.charAt(0); } } }
?
? http://javaflex.iteye.com/?
??