1. 程式人生 > 其它 >技術分享 | 黑盒測試方法論—邊界值

技術分享 | 黑盒測試方法論—邊界值

順序結構

  • Java的基本結構就是順序結構,除非特別指明,否則就按照順序一句一句執行

  • 順序結構是最簡單的演算法結構

  • 語句與語句之間,框與框之間是按照從上到下的順序進行的,他是由若干個依次執行的處理步驟組成的,他是任何一個演算法都離不開的一種基本演算法結構


 

順序結構

 

public class Dome01 {
   public static void main(String[] args) {
       //創造一個掃描器物件,用於接收鍵盤資料   A|t + 回車鍵
       Scanner scanner = new Scanner(System.in);     //System.in為輸入   System.out為輸出

       System.out.println("使用next方式接收:");

       //判斷使用者有沒有輸入字串
       if(scanner.hasNext()){
           //使用next方式接收
           String str = scanner.next();
           System.out.println("輸出的內容為:"+str);
      }

       //凡是屬於IO流的類如果不關閉會一直佔用資源,要養成好習慣用完就關掉
       scanner.close();
  }
}

 

 

順序結構(line)(常用)

public class Dome02 {
   public static void main(String [] args) {
       Scanner scanner = new Scanner(System.in);
       //從鍵盤接收資料
       System.out.println("使用nextLine方式接收:");

       //判斷是否還有輸入
       if (scanner.hasNextLine()) {
           String str = scanner.nextLine();
           System.out.println("輸出的內容為:"+str);
      }
       scanner.close();
  }
}

 

 

選擇結構

public class Dome04 {
   public static void main(String[] args){
       Scanner scanner = new Scanner(System.in);

       //從鍵盤接收資料
       int i = 0;
       float f = 0.0f;

       System.out.println("請輸入整數:");

       //如果.....那麼...
       if (scanner.hasNextInt()){
           f = scanner.nextInt();
           System.out.println("整數資料:"+ f);
      }else{
           System.out.println("輸入的不是整數資料!");
      }


       System.out.println("請輸入小數:");
       if (scanner.hasNextFloat()){
           f = scanner.nextFloat();
           System.out.println("小數資料:"+ f);
      }else{
           System.out.println("輸入的不是小數資料!");
      }

       System.out.println(i);

       scanner.close();
  }
}

 

 

迴圈結構

public class Dome05 {
   public static void main(String[]args){
       //我們可以輸入多個數字,並求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果

       Scanner scanner = new Scanner(System.in);

       //和
       double sum = 0;
       //計算輸入了多少個數字
       int m = 0;

       System.out.println("請輸入一個整數:");


       //通過迴圈判斷是否還有輸入,並在裡面對每一次進行求和和統計
       while(scanner.hasNextDouble()){
           double x = scanner.nextDouble();
           m = m+1;   //m++
           sum = sum+x;
           System.out.println("你輸入了第"+m+"個數,當前數字為sum="+sum);
      }
       System.out.println(m + "個數的和為" + sum );
       System.out.println(m + "個數的平均值是" + (sum / m));

       scanner.close();
  }
}

 

 

 

選擇結構

 

if單選擇結構

  • 我們很多時候需要判斷一個東西是否可行,任何我們才去執行,這樣一個過程在程式中用if語句來表示

語法:

if(布林表示式){

//如果布林表示式為true將執行的語句

}

if雙選擇結構

  • 那現在有個需求,公司要收購一個軟體,成功了,給人支付一百萬,失敗了,自己找人開發,這樣的需求用一個if就搞不定了,我們需要有兩個判斷,需要一個雙選擇結構,所以就有了if-else結構

語法:

if(布林值表示式){

//如果布林表示式為true

}else{//如果布林表示式為false

}

 

if多選擇結構

  • 我們發現剛才的程式碼不符合實際情況,真實的情況還存在ABCD,存在區間多級判斷,比如90-100就是A,80-90就是B等等,在生活中我們很多時候的選擇也不僅僅只有兩個選擇,所以我們需要一個多選擇結構來處理這類問題!

public class IfDemo03 {
   public static void main(String[] args){
       Scanner scanner = new Scanner(System.in);
       System.out.println("請輸入成績:");
       int score = scanner.nextInt();

       if (score==100){
           System.out.println("恭喜滿分!");
      }else if(score<100 && score>=90){
           System.out.println("A級");
      }else if (score<90 && score>=80){
           System.out.println("B級");
      }else if (score<80 && score>=70){
           System.out.println("C級");
      }else if (score<70 && score>=60){
           System.out.println("D級");
      }else if (score<60 && score>=0 ){
           System.out.println("不及格!");
      }else{
           System.out.println("非法成績");
      }

       scanner.close();
  }
}