爲SCJP認證考試而努力-5

新的章節,流程控制和差錯處理。
 
package control;

/**
* If和Switch兩個流程控制語句
* @author 木炭
*
*/

public class IfSwitch {

  /**
    * @param args
    */

  public static void main(String[] args) {
    boolean condition = true;
    if (condition) {//if 語句只能用boolean 類型參數
      // condition爲true時做一些事情
    } else {
      // 其他情況做一些事情
    }

    int k = 10;
    switch (k) {//switch 語句只能用byte,char,short或者int 類型作參數
    case 10:
      System.out.println("ten");
      break;// 如果不使用break,語句就會一直執行下去
    case 20:
      System.out.println("twenty");
      break;
    default:// default語句可以在case前面
      System.out.println("This is the default output");
    }
  }
}
 
練習

創建一個文件含有一個公共類叫IfElse。創建一個方法叫go,它接收main 方法的字符串數組
參數作爲它的參數。在這個方法中創建了一個if/else 程序塊,使if 語句可以檢查傳到go 方
法的字符串數組是否是零長度串,使用數組length 域來檢查。如果長度爲零則輸出
"沒有提供參數",在它的else塊中,建立if/else if/else 塊,用來查看來自數組的第一個元
素,用字符串的equals 方法來判斷輸出。如果爲"true"則打印"ok",如果爲"false"則打印
"Notok",如果是true 或false 以外的字符串則打印"無效的命令參數",用一個if/else
if/else 語句這樣的次序進行設計。
 
package control;

/**
* ifElse流程控制語句
* @author 木炭
*
*/

public class IfElse {
  public static void main(String args[]) {
    IfElse ie = new IfElse();
    ie.go(args);
  }

  public void go(String[] sa) {
    if (sa.length == 0) {
      System.out.println("沒有提供參數");
    } else {
      String s = sa[0];
      if (s.equals("true")) {
        System.out.println("OK");
      } else if (s.equals("false")) {
        System.out.println("Not OK");
      } else {
        System.out.println("無效的命令參數");
      }
    }
  }
}
 
問題 ) 編譯運行下列代碼時會發生什麼情況?
public class MyIf{
boolean b;
public static void main(String argv[]){
  MyIf mi = new MyIf();
}
MyIf(){
  if(b){
    System.out.println("The value of b was true");}
  else{
    System.out.println("The value of b was false");}
}
}
1) Compile time error variable b was not initialised
2) Compile time error the parameter to the if operator must evaluate to a boolean
3) Compile time error, cannot simultaneously create and assign value for boolean value
4) Compilation and run with output of false
答案4) Compilation and run with output of false
因爲boolean b 在類級中被創建,它不需明確初始化,而且它有默認的boolean 值false。if
語句判斷一個boolean 值,所以b 符合這個要求。
問題 ) 編譯運行下列代碼時會發生什麼情況?
public class MyIf{
  public static void main(String argv[]){
    MyIf mi = new MyIf();
  }
  MyIf(){
  boolean b = false;
  if(b=false){
    System.out.println("The value of b is"+b);}
  }
}
1) Run time error, a boolean cannot be appended using the + operator
2) Compile time error the parameter to the if operator must evaluate to a boolean
3) Compile time error, cannot simultaneously create and assign value for boolean value
4) Compilation and run with no output
答案4) Compilation and run with no output//沒看下面的解釋之前我也想不通呢
因爲b 是boolean 類型,if 語句不會產生錯誤。如果b 是任何其他的數據類型,在你試圖賦
值而不是比較的時候錯誤就產生了。下列表達
if(b=false)
通常是一個程序員的錯誤。程序員大多要表現
if (b==false)
如果b 的類型是boolea 以外的任意類型,會導致編譯期錯誤。if 表達式的要求是必須返回一
個boolean 類型,因爲
(b=false )
返回一個boolean 類型,所以被接受。
問題) 下面哪個是不能用於switch 語句的參數?
1) byte b=1;
2) int i=1;
3) boolean b=false;
4) char c='c';
答案
3) boolean b=false;
switch 語句可以使用byte,char 或int 作參數。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章