频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
java学习笔记 之 流程控制
2011-11-18 13:50:34           
收藏   我要投稿

 

先说一下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);

                            }

                     }

              }

 

 

 

 

 

\

 

摘自 pcenshao

点击复制链接 与好友分享!回本站首页
相关TAG标签 流程 笔记
上一篇:Java去除两个日期之间的周末,只算工作日(传递String对象)
下一篇:一个Java程序员应该掌握的10项技能
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站