JAVA學習第一節 2011-7-19下午

 小知識點歸納:

main()是類體中的主方法。public。static 。void分別是main方法的權限修飾符,靜態修飾符,返回值修飾符。

求餘數的符號是 %       取整的符號是 [ ]。

實踐與練習1:判斷變量x是奇數還是偶數。

public class Example1 {
 public static void main(String args[]){
       int x=2;
       if(x%2==0){
        System.out.println("x is even");
       }
       else{
        System.out.println("x is odd");
       }
 }
}


           練習2:運用for循環打印菱形。           

public class Example2 {
 public static void main(String[] args) {
    int i,j,k;                       //i represent row,j represent column,k represent the number of *
    for(i=0;i<=4;i++){
      for(j=0;j<4-i;j++){
      System.out.print(" ");
        }
      for(k=1;k<=2*i+1;k++){
       System.out.print("*");
      }
      System.out.println();
    }                                   //above mean print the first five rows
    for(i=0;i<=3;i++){
     for(j=1;j<=i+1;j++){
      System.out.print(" ");
     }
     for(k=1;k<8-2*i;k++){
      System.out.print("*");
     }
     System.out.println();
    }
 }
}

想這個想了很多方法,覺得自己沒什麼耐心,事實上這個過程中我覺得能學到很多,分析問題的方法 。這個還是有爭論的,
 
   練習3:使用while循環語句計算1!+1/2!+1/3!+.......+1/20!之和。    
public class Example {
  public static void main(String[] args) {
     double sum=0;
     double result;
     double b=1.5;
     int x=1; 
     result=1;
     while(x<=20){
      result=result*x;
         b=1/result;
      sum=sum+b;
      x=x+1;  
         System.out.println(b);
           }
     System.out.println(sum);
 }
}

做這題的時候,變量的類型定義 一定要清楚啊,不然就很容易出錯了,還是學到挺多東西的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值