为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 作参数。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章