作業題.,

來源:互聯網
上載者:User

作業題.,

1.

public class T
{
  public static void main (String[] args){
  System.out.println("5+5="+5+5);
  }

}

2.(a++是先使用後自增, ++a是先自增後使用 )

(1)

public class G
{
  public static void main (String[] args){
  int a=3,b;
  b=a++;
  System.out.println("a="+a+",b="+b);
  }

}

(2).

public class H
{
  public static void main (String[] args){
  int a=3,b;
  b=++a;
  System.out.println("a="+a+",b="+b);
  }

}

3.

public class I
{

  public static void main (String[] args){
  short s=3;
  s+=4;      //s是short類型的 4是int類型   +=是指派陳述式相當於=層級
  System.out.println("s="+s);

  }

}

4.位元運算符計算

public class J
{
  public static void main (String[] args){
  System.out.println(6&3);
  System.out.println(6|3);
  System.out.println(6^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補
  }

}

5.

public class K
{
  public static void main (String[] args){
  int x=0,y;
  y=x>1?100:200;
  System.out.println("y="+y);
  }

}

練習.

(1).輸出兩個整數中較大的整數

 

public class M
{
public static void main (String[] args){
int a=1,b=0,max;
max=a>b?a:b;
System.out.println("max="+a);
}

}

(2) 輸出三個整數中最大的整數

 

public class M2
{
  public static void main (String[] args){
  int a=1,b=0,c=3,max;
  max=a>b?a:b;
  max=c>max?c:max;
  System.out.println("max="+c);
 }

}

 

 

6.(1)直接使用一個if語句

public class N
{
  public static void main (String[] args){
  int weekDay=3;
  if(weekDay==1){
    System.out.println("今天是星期一");
  }
  if(weekDay==2){
    System.out.println("今天是星期二");
  }
  if(weekDay==3){
    System.out.println("今天是星期三");
  }
  if(weekDay==4){
    System.out.println("今天是星期四");
  }
  if(weekDay==5){
    System.out.println("今天是星期五");
  }
  if(weekDay==6){
    System.out.println("今天是星期六");
  }
  if(weekDay==7){
    System.out.println("今天是星期七");
  }
  System.out.println(weekDay);
  }

}

(2)使用關聯的if語句

public class O
{
  public static void main (String[] args){
  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("沒有這一天");
  }
  }
}

練習.

public class P
{
  public static void main (String[] args){
  int month=3;
  if(month==1){
    System.out.println("這個月是冬天");
  }
  if(month==2){
    System.out.println("這個月是冬天");
  }
  if(month==3){
    System.out.println("這個月是春天");
  }
  if(month==4){
    System.out.println("這個月是春天");
  }
  if(month==5){
    System.out.println("這個月是春天");
  }
  if(month==6){
    System.out.println("這個月是夏天");
  }
  if(month==7){
    System.out.println("這個月是夏天");
  }
  if(month==8){
    System.out.println("這個月是夏天");
  }
  if(month==9){
    System.out.println("這個月是秋天");
  }
  if(month==10){
    System.out.println("這個月是秋天");
  }
  if(month==11){
    System.out.println("這個月是秋天");
  }
  if(month==12){
    System.out.println("這個月是冬天");
  }

  System.out.println(month);
  }

}

7.使用分支語句,計算+ - * / %, 使用switch語句

 

public class Q{
  public static void main(String[] args){
  int a=84,b=3;
  char option='%';
  switch (option)
  {
  case '+':
    System.out.println("a+b="+(a+b));
    break;
  case '-':
    System.out.println("a-b="+(a-b));
    break;
  case '*':
    System.out.println("a*b="+(a*b));
    break;
  case '/':
    System.out.println("a/b="+(a/b));
    break;
  case '%':
    System.out.println("a%b="+(a%b));
    break;
  default:
    System.out.println("a%b="+(a%b));
    break;
  }
 }
}

練習

public class R{
  public static void main(String[] args){
  int month=3;
  switch (month)
  {
  case 1:
    System.out.println("這個月是冬天");
    break;
  case 2:
    System.out.println("這個月是冬天");
    break;
  case 3:
    System.out.println("這個月是春天");
    break;
  case 4:
    System.out.println("這個月是春天");
    break;
  case 5:
    System.out.println("這個月是春天");
    break;
  case 6:
    System.out.println("這個月是夏天");
    break;
  case 7:
    System.out.println("這個月是夏天");
    break;
  case 8:
    System.out.println("這個月是夏天");
    break;
  case 9:
    System.out.println("這個月是秋天");
    break;
  case 10:
    System.out.println("這個月是秋天");
    break;
  case 11:
    System.out.println("這個月是秋天");
    break;
  case 12:
    System.out.println("這個月是冬天");
    break;
  default:
    System.out.println("沒有這個月");
    break;
  }
 }
}

8.(1)

public class S{
  public static void main(String[] args){
  int x=1;
  do{
    System.out.println("x="+x);
    x++;
  }while(x<1);
  System.out.println(x);
 }
}

(2)

public class S2{
  public static void main(String[] args){
  int y=1;
  while(y<1){
    System.out.println("y="+y);
    y++;
  }
  System.out.println(y);
 }
}

 

 9

(1)

 

public class U1{
  public static void main(String[] args){
  for(int x=1;x<3;x++)
  {
    System.out.println("x="+x);
  }
   }
}

(2)

 

public class U2{
  public static void main(String[] args){
  for(System.out.println("a");System.out.println("b");System.out.println("c"))
  {
    System.out.println("d");
  }
   }
}

聯繫我們

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