1. 程式人生 > 其它 >golang執行時庫runtime:Caller()方法

golang執行時庫runtime:Caller()方法

1.Scanner

1.1 scanner

package com.scanner;

import java.util.Scanner;

public class Demo01 {
  public static void main(String[] args) {
      //建立一個掃描物件,用於接收鍵盤資料
      Scanner scanner= new Scanner(System.in);

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

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

      //凡是屬於IO流的類如果不會關關閉會一直佔用資源,要用完關閉
      scanner.close();

  }
}
package com.scanner;

import java.util.Scanner;

public class Demo02 {
  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();
  }
}
package com.scanner;

import java.util.Scanner;

public class Demo03 {
  public static void main(String[] args) {
      //從鍵盤接收資料
      Scanner scanner= new Scanner(System.in);

      System.out.println("請輸入資料:");
          //判斷是否還有輸入
          String str =scanner.nextLine();
          System.out.println("輸出內容為:"+str);

      scanner.close();
  }
}
package com.scanner;

import java.util.Scanner;

public class Demo04 {
  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()) {
          i = scanner.nextInt();
          System.out.println("整數資料:" + i);
      }else{
              System.out.println("輸入的不是整數資料:");

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


          scanner.close();
      }
      }






}
package com.scanner;

import java.util.Scanner;

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

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

      //通過迴圈判斷是否還有輸入,並在裡面對每一次進行求和統計
      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();


  }

}

2.順序結構

package com.struct;

public class Order01 {
  public static void main(String[] args) {
      System.out.println("0.1");
      System.out.println("0.2");
      System.out.println("0.3");
      System.out.println("0.4");
      System.out.println("0.5");
      System.out.println("0.6");
  }
}

3.選擇結構

3.1 If選擇結構

 

package com.struct;

import java.util.Scanner;

public class IfDemo01 {
  public static void main(String[] args) {
      Scanner scanner = new Scanner (System.in);
      System.out.println("請輸入內容:");

      String s = scanner.nextLine();

      //equals:判斷字串是否相等
      if (s.equals("hello")){

          System.out.println(s);
      }
      System.out.println("end");
      scanner.close();
  }
}

 

package com.struct;

import java.util.Scanner;

public class IfDemo02 {
  public static void main(String[] args) {
      //考試分數大於60就是及格,小於60則不及格
      Scanner scanner = new Scanner(System.in);

      System.out.println("請輸入內容:");

      int score =scanner.nextInt();
      if (score>=60){
          System.out.println("及格");


      }else {
          System.out.println("不及格");
      }






      scanner.close();
  }
}
package com.struct;

import java.util.Scanner;

public class IfDemo03 {
  public static void main(String[] args) {
      Scanner scanner =new Scanner(System.in);
      /*
      * if 語句至多有1個 else 語句 else語句在所有的elseif 語句         之後。
      * if 語句可以有若干個 else if 語句,他們必須在else 語句之           前。
      * 一旦其中一個 elseif 語句檢測為true,其他的 elseif以及         else語句都將跳過執行。
      */


      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();
  }
}

3.2 Switch選擇結構

package com.struct;

public class SwitchDemo01 {
  public static void main(String[] args) {

      //case穿透 //SWITCH 匹配一個具體的值
      String grade = "K";
      // 反編譯 java ------class (位元組碼檔案)----反編譯(IDEA)
      switch (grade){
          case "A":
              System.out.println("優秀");
              break;//可選
          case "B":
              System.out.println("良好");
              break;//可選
          case "C":
              System.out.println("及格");
              break;//可選
          case "D":
              System.out.println("再接再厲");
              break;//可選
          case "E":
              System.out.println("掛科");
              break;//可選
          default:
              System.out.println("????");
      }

  }

}

4.迴圈結構

4.1 while迴圈詳解

package com.struct;

public class WhileDemo01 {
  public static void main(String[] args) {
      //輸出1~100

      int i =0;

      while ( i<100){
          i++;
          System.out.println(i);
      }
  }
}

 

package com.struct;

public class WhileDemo02 {
  public static void main(String[] args) {
      //死迴圈
      while (true){
          //等待伺服器響應
          //定時檢查
      }
  }
}
package com.struct;

import java.sql.SQLOutput;

public class WhileDemo03 {
  public static void main(String[] args) {
    //計算1+2+3+...+100=?

    int i =0;
    int sum = 0;

    while (i<=100) {
        sum = sum + i;
        i++;
    }
      System.out.println(sum);
  }

}
package com.struct;

public class WhileDemo04 {
  public static void main(String[] args) {
      //練習2:用while或for迴圈輸出1-1000之間能被5整除的數,並且每行輸出3個
      int i = 0;
      while (i <= 1000) {
          if (i % 5 == 0 && i != 0) {
              System.out.print(i + "\t");

              }
          if (i % (5 * 3) == 0) {//每行輸出3個
              System.out.println();

          }

          i++;
      }
  }
}

 

4.2 DoWhile迴圈

package com.struct;

public class DoWhileDemo01 {
  public static void main(String[] args) {
      int a = 0;
      int sum =0 ;
      do {
          sum = sum+a;
          a++;

      }while (a<=100);
      System.out.println(sum);

  }

}

 

package com.struct;

public class DoWhileDemo02 {
  public static void main(String[] args) {
      int a = 0;
      while (a<0){
          System.out.println(a);
          a++;
      }
      System.out.println("-----------");
      do{
          System.out.println(a);
          a++;
      }while (a<0);
  }
}

 

4.3 For迴圈

package com.struct;

public class ForDemo01 {
  public static void main(String[] args) {
      int a = 1;//初始化條件
      while (a<=100) {//條件判斷
          System.out.println(a);//迴圈體
          a += 2;//迭代
      }
      System.out.println("While迴圈結束!");
          //初始化/ /條件判斷 //迭代
      for (int i = 0; i <= 100; i++) {
          System.out.println(i);
      }
      System.out.println("For迴圈結束!");
      /*
      * 關於 for 迴圈有以下幾點說明:
      * 最先執行初始化步驟,可以宣告一種型別,但可以初始化多給迴圈一           個或控制變數,也可以是空語句。
      * 然後,檢測布林表示式的值。如果為true,迴圈體被執行。如果為 false,迴圈結束,開始執行迴圈體後面的語句。
      * 執行溢位迴圈後,更新迴圈控制變數(迭代因子控制迴圈變數的增減)。
      * 再次檢測布林表示式。迴圈執行上面的過程。

      * */
      //死迴圈
      for (;;){

      }
  }
}
package com.struct;

public class ForDemo02 {
  public static void main(String[] args) {
      //練習1:計算0到100之間的奇數和偶數的和

      int oddSum = 0;
      int evenSum = 0;

      for (int i = 0; i <= 100; i++) {
          if (i % 2 != 0) {//奇數
              oddSum += i; //oddSum = oddSum+ i;

          } else {
              evenSum += i;
          }

      }
      System.out.println("奇數的和:" + oddSum);
      System.out.println("偶數的和:" + evenSum);
  }
}
package com.struct;

public class ForDemo03 {
  public static void main(String[] args) {
      //練習2:用while或for迴圈輸出1-1000之間能被5整除的數,並且每行輸出3個
      for (int i = 0; i <= 1000; i++) {
          if (i%5==0&&i!=0){
              System.out.print(i+"\t");

          }
          if (i%(5*3)==0) {//每行輸出3個
              System.out.println();
              //System.out.print("\n");輸出一個換行符
              //println 輸出完會換行
              //print 輸出完不會換行
          }
      }
  }
}
package com.struct;

public class ForDemo04 {
  public static void main(String[] args) {
      //1.我們先列印第一列
      //2.我們把固定的1再用一個迴圈包起來
      //3.去掉重複的項,i<=j
      //4.調整樣式
      for (int j = 1; j <= 9; j++) {
          for (int i = 1; i <= j; i++) {
          System.out.print(j+"*"+i+"="+(j*i)+"\t");
      }
          System.out.println();
      }
  }
}
package com.struct;

public class ForDemo05 {
  public static void main(String[] args) {
      int[] numbers ={10,20,30,40,50};
      for (int i = 0;i<5;i++){
          System.out.println(numbers[i]);
      }
      System.out.println("=============");
      //遍歷陣列元素
      for (int x:numbers){
          System.out.println(x);
      }
  }
}

 

 

5.break & contiue goto

5.1 break

package com.struct;

public class BreakDemo {
  public static void main(String[] args) {
      int i = 0;
      while (i < 100) {
          i++;
          System.out.println(i);
          if (i == 30) {
              break;
          }
      }
      System.out.println("456");
  }
}

5.2 contiue

package com.struct;

public class ContinueDemo {
  public static void main(String[] args) {
      int i = 0;
      while (i<100){
          i++;
          if (i%10==0){
              System.out.println();
              continue;
          }
          System.out.print(i);
      }
  }//break在任何迴圈語句的主體部分,均可用break控制迴圈流程。
  // break用於強行退出迴圈,不執行迴圈中剩下的語句。(break語句葉在switch語句中使用)
  //
  //continue 語句用在迴圈語句中,用於終止某次迴圈過程,即跳過迴圈體中尚未執行的語句,接著進行下一次是否執行迴圈的判定。
}

5.3 go to

package com.struct;

public class LabelDemo {
  public static void main(String[] args) {
      //列印101-150之間的所有質數
      //質數是指在大於1的自然數中,除了1和它本身以外不再有其他因素的自然數。

      int count = 0;
      //不建議使用
      Outer:for (int i =101;i<150;i++){
          for (int j =2 ; j<i/2;j++){
              if (i % j==0){
                  continue Outer;
              }
          }
          System.out.print(i+" ");
      }

  }
}

6.練習

列印三角形 6行

package com.struct;

public class TestDemo01 {
  public static void main(String[] args) {
      //列印三角形 6行
      for (int i = 1; i <= 6; i++) {
          for (int j = 5; j >= i; j--) {
              System.out.print(" ");

          }
          for (int j = 1; j <= i; j++) {
              System.out.print("@");
          }
          for (int j =1; j<i;j++){
              System.out.print("@");
          }
          System.out.println();


      }
  }
}