java程式設計國慶作業

來源:互聯網
上載者:User

標籤:images   聯絡   str   語句   short   國慶   整數   option   迴圈   

public class X{
  public static void main(String[] args){
    //第一題
    System.out.println("5+5="+5+5);
    //第二題
    int a=3,b;
    b=a++;
    System.out.println("a="+a+",b="+b);
    //第三題
    short s=3;
    s=(short)(s+4);//不相容的類型: 從int轉換到short可能會有損失.+short
    System.out.println("s="+s);
    //第四題位元運算符
    System.out.println(6&3);//無條件與
    System.out.println(6|3);//無條件或
    System.out.println(3^3);//當不一樣的時候為真 1和0為真 一樣的時候為假 0和0 1和1
    System.out.println(3<<2);//左移 實際上是用來計算3*2^2=12
    System.out.println(3>>1);//右移 實際上是用來計算除法 3/(2^1)=1//sop(3>>>1)也是右移,不同之處在於出現的空位都用0補
    //第五題三元運算子
    int x=3,y;
    y=x>1?100:200;//三元條件,判斷x是否大於1,若是y=100,若不是y=200,右結合性
    System.out.println("y="+y);
    //用三元運算子做聯絡
    int z=15,q=10,v=8,e;
    System.out.println(e=z>q?z:q);//輸出兩個整數中較大的整數
    System.out.println(e=z>q?z:q>v?q:v);//輸出三個整數中最大的整數
    //第六題判斷語句
    int weekDay=3;//判斷星期
    if(weekDay==1){
      System.out.println("今天是星期一");
    }
    else if(weekDay==2){
      System.out.println("今天是星期二");
    }
    else if(weekDay==3){
      System.out.println("今天是星期三");
    }
    else if(weekDay==4){
      System.out.println("今天是星期四");
    }
    else if(weekDay==5){
      System.out.println("今天是星期五");
    }
    else if(weekDay==6){
      System.out.println("今天是星期六");
    }
    else if(weekDay==7){
      System.out.println("今天是星期日");
    }
    else{
      System.out.println("沒有這一天");
    }
    int month=8;//判斷季節
    if(month==3||month==4||month==5){
      System.out.println("現在是春天");
    }
    else if(month==6||month==7||month==8){
      System.out.println("現在是夏天");
    }
    else if(month==9||month==10||month==11){
      System.out.println("現在是秋天");
    }
    else if(month==12||month==1||month==2){
      System.out.println("現在是冬天");
    }
    else{
      System.out.println("沒有這個季節");
    }
    //第七題使用分支語句
    int c=84,h=3;
    char option=‘+‘;
    switch (option)
    {
    case ‘+‘:
      System.out.println("c+h="+(c+h));
      break;
    case ‘-‘:
      System.out.println("c-h="+(c-h));
      break;
    case ‘*‘:
      System.out.println("c*h="+(c*b));
      break;
    case ‘/‘:
      System.out.println("c/h="+(c/h));
      break;
    case ‘%‘:
      System.out.println("c%h="+(c%h));
      break;
    default:
      System.out.println("c%h="+(c%h));
      break;
    }
    //使用switch判斷季節
    int mon=8;
    switch(mon)
    {
    case 3:
    case 4:
    case 5:
      System.out.println("現在是春天");
      break;
    case 6:
    case 7:
    case 8:
      System.out.println("現在是夏天");
      break;
    case 9:
    case 10:
    case 11:
      System.out.println("現在是秋天");
      break;
    case 12:
    case 1:
    case 2:
      System.out.println("現在是冬天");
      break;
    default:
      System.out.println("沒有這個季節");
      break;
    }
    //第八題do while和while的區別
    int d=1;
    do{
      System.out.println("d="+d);
      d++;
    }while(d<1);//不管while裡面的條件是否成立,do裡面的都要運行
    int f=1;
    while(f<1){
      System.out.println("f="+f);
      f++;
    }
    //第九題使用for語句寫一個簡單的迴圈語句
    for(int g=1;g<5;g++)
    {
      System.out.println("g="+g);
    }
  }
}

 

java程式設計國慶作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.