java新手筆記3

來源:互聯網
上載者:User

標籤:

1.包

2.運算子

public class Operator {     public static void main(String[] args) {       int a = 5;       System.out.println("a = " + a);       //a = -a; //+   -       System.out.println("a = " + a);       //+ 字串連結       System.out.println("影分身" + "軟體開發");       System.out.println(3 + 5 + "軟體開發" + 6 + 8);       double b = a / 2;       System.out.println("a / 2 = " + (a / 2));       System.out.println("b = " + b);       int weith = 10;       int height = 6;       double area =  1.0 / 2 * weith * height;       System.out.println("area = " + area);       int c = a % 3;//取餘數       System.out.println("c = " + c);       //int d = c++;//  c++  c = c + 1; 先賦值 後自增       int d = ++c;//先自增 後賦值       System.out.println("d = " + d);       System.out.println("c = " + c);       //賦值         int e = 10; //-=  *= /= %=       e += 1;// e = e + 1;       System.out.println("e = " + e);       //比較 > >=  5 >= 5  5 > 5  < <=  ==       boolean isEquals = (1 == 1);       System.out.println("isEquals = " + isEquals);       int year = 2012;       boolean isLeap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);        System.out.println(year +" 是否是閏年 " + isLeap);       System.out.println("1 != 2 ? " + (1 != 2));       //邏輯       boolean isTrue = false;       System.out.println("isTrue ? " + isTrue);       System.out.println("!isTrue ? " + !isTrue);       System.out.println("true || false ? " + (true || false));//或運算       System.out.println("true && false ? " + (true && false));//與運算       int score = 85;       //score > 60  及格   > 70  良   > 85 優秀       // javase > 60 && oracle > 70  && xml > 70        // javase > 60 || oracle > 60  || xml > 60        int move = 16;       System.out.println("move = " + (move >> 3));//100   10       System.out.println("move = " + (move << 1));         }}

 3.選擇語句

import java.util.Random;//匯入類public class IfDemo {     public static void main(String[] args) {      Random ran = new Random();//new 建立對象      int score = ran.nextInt(101);//ran.nextInt(41) + 60;//隨機數 0 ~ 100      System.out.println("score = " +  score);      /*      if(score > 60) {//滿足條件執行         System.out.println("============");         System.out.println("   及格了..");         System.out.println("============");      }      */      //if的第二種形式      /*      if(score > 60) {//滿足條件執行           System.out.println("============");         System.out.println("   及格了..");         System.out.println("============");      } else {  //不滿足條件執行         System.out.println("============");         System.out.println("   很遺憾..");         System.out.println("============");      }      */      //多分支  匹配其中一個條件      /*      if( score >= 90) {         System.out.println("成績: A");      } else if ( score >= 80 /*&& score < 90) {         System.out.println("成績: B");      } else if ( score >= 60 /*&& score < 80) {         System.out.println("成績: C");      } else {         System.out.println("成績: D");      }      */      switch(score / 10) {//如果匹配 下面代碼執行        case 10: //System.out.println("成績: A");break;        case 9: System.out.println("成績: A");break;        case 8: System.out.println("成績: B");break;        case 7: System.out.println("成績: C");break;        case 6: System.out.println("成績: D");break;        default : System.out.println("成績: 不及格");      }      //三目運算子      boolean isPass = ( score > 60 ? true : false);      System.out.println("成績 === " + isPass);      String passed = score > 60 ? "及格" : "掛了";      System.out.println("成績 ==== " + passed);         }}

 4.while迴圈

import java.util.Random;public class LoopDemo {     public static void main(String[] args) {              int i = 0;//計數器       //迴圈  不滿足條件不執行   可能執行0次       while( i < 10 ){               System.out.println( i + " Hello World!...");//執行代碼          i++;//運行時 i值每次發生改變 i = 10       }               System.out.println( "i = " + i );       i = 0; // 0  9       do{//先執行 後判斷  至少執行一次          System.out.println( i + " 快樂!...");          i++;       }       while (i < 10);              int a = ran.nextInt(101);       System.out.println( "a = " + a );       int i = 2;       while( i < a) {//67          if( a % i == 0) {             break;//跳出迴圈          }          i++;       }       System.out.println( "i = " + i );       if(i >= a)//正常退出迴圈          System.out.println( a +"是素數");       else          System.out.println( a +"不是素數");    }}

 5.for迴圈

import java.util.Random;public class LoopDemo2 {     public static void main(String[] args) {      int k = 10; //方法中的變數 範圍 在方法中使用      //i 局部變數            for(int i = 0;i < 10 ; i++ ) {//三條語句         System.out.println( i + " Hello World!...");               }            //迴圈嵌套      /*      for(int i = 0; i < 5; i++) {         //k = k + i;         for(int j = 0; j < 3; j++) {            System.out.print(" * ");         }         System.out.print("\n");//輸出換行      }      */      for(int i = 1; i < 6; i++) {         for(int j = 1; j <= i; j++) {            System.out.print(j + " * " + i + " = "+ i * j + "  ");         }         System.out.print("\n");      }           int sum = 0;      int i;      for(i = 1; i < 200; i++) {         sum += i;         if(sum >= 200){            break;         }            }      System.out.println("sum = " + sum);      System.out.println("i = " + i);    }}

 6.標記迴圈

import java.util.Random;public class LoopDemo3 {     public static void main(String[] args) {           int sum = 0;      int i,k = -1;      loopi: for(i = 1; i < 200; i++) {         for(int j = 1; j < 200; j++) {            sum += j;            if(sum >= 200){                k = j;               break loopi;//指定跳出位置            }                     }         System.out.println("k = " + k);         System.out.println("i = " + i);      }      System.out.println("sum = " + sum);      System.out.println("k = " + k);      for(int a = 1; a < 101; a++ ) {         if( a % 3 != 0){//不能被3整除              continue;//結束本次後面代碼執行         }         System.out.print( a + "\t");      }          }}

 7.雙迴圈

public class LoopDemo4 {     public static void main(String[] args) {           int sum = 0;            for(int i = 1; i < 11; i++) {//控制的是行         for(int j = 1; j <= i; j++) {//控制的是列            System.out.print(" * ");            /**/            if(j > 5){ //1 2 3 4 5 6   7 8 9 10                          break;//跳出內層迴圈            }                       }                  System.out.println();      }          }}

 

java新手筆記3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.