java學習筆記 之 流程控制

先說一下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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章