2015年10月19日 星期一

進階練習一

Debug the following code segments. Remove all syntax and logical errors.

/***********************************************************/

1. The following code should print the word Tom ten times)

x=0;
while(x<10)
    System.out.println("Tom");
    x++;

/***********************************************************/

2. The following code should read an integer x inputted by user and print it.

Scanner input = new Scanner();
int x = input.nextLine();
System.out.println(x);


/***********************************************************/

3. The following code should assign the value of x to be the square root of y.

int x = sqrt(y);



/***********************************************************/


4. The following code should print all even integers from 50 to 100

for( i=2 ; i<=100; i+=2)
    System.out.println(i+50);


/***********************************************************/

2015年10月12日 星期一

For loop Ex1


1. for(int i=0; i<3 ; i++){}



2. for(int i=1; i<3 ; i++){}


3. for(int i=0; i<13 ; i++){}


4. for(int i=1; i<3 ; i++){}


5. for(int i=0; i<=13 ; i++){}

-----------------------------------------

6. for(int i=0; i<=10 ; i++){}


7. for(int i=0; i<20 ; i++){}


8. for(int i=1; i<10 ; i++){}


9. for(int i=0; i<=15 ; i++){}


10. for(int i=0; i<100 ; i++){}


-----------------------------------------


11. for(int i=0; i<99 ; i++){}


12. for(int i=0; i<=100 ; i++){}


13. for(int i=1; i<=100 ; i++){}


14. for(int i=1; i<100 ; i++){}


15. for(int i=0; i<=99 ; i++){}



2015年3月12日 星期四

java 練習一 ans



------------------------------------------------

1)

1013
11
-9

------------------------------------------------

2)

-2
[ actually, -2 will not print because error will happen in --> int c = a/=0; ]


------------------------------------------------



3.1)

*
*
*
*
*

------------------------------------------------

3.2)

*****

------------------------------------------------

4)

* * * * * *
* * * *

------------------------------------------------


5.1)  5
5.2)  6
5.3)  5
5.4)  10
5.5)  2
5.6)  10
5.7)  30
5.8)  33
5.9)  error [ 變量j的生存空間只在於第二個for loop, 所以最後System.out.println(j); 會error ]


------------------------------------------------


6.1)

*
**
***
****
*****

------------------------------------------------


6.2)

_ _ _ _ *
_ _ _ * *
_ _ * * *
_ * * * *
* * * * *


------------------------------------------------







java 練習二




1.

    int a = 10;
    int b = a++;
    int c = ++a;

    System.out.println(b);
    System.out.println(a++);
    System.out.println(a/=b);
    System.out.println(a%b);
    System.out.println(a);
 

-----------------------------------------------------------

2.
  Answer how many star will be generated?
 


2.1
        for(int i=0 ; i<15; i++)
             System.out.print("*");



2.2
 
          for(int i=0; i<=15; i++){}
               System.out.print("*");



2.3
          for(int i=1; i<15; i++){
                   System.out.print("*");
          }



2.4

         for(int i=1; i<10; i++);
               System.out.print("*");


            

2.5

          for(int i=0, a=1; a<5; i++)
                System.out.print("*");




-------------------------------------------------------------------------

3. Answer how many star will be created


 3.1

           for(int i=5; i>0 ; i--)
                     System.out.print("*");




3.2

           for(int i=5; 0<i ; i--)
                   System.out.print("*");
           




3.3

          for(int a=5; a>0 ; a--) {
                    System.out.println("*");
         }






3.4

        for(int i=10, i<0 ; i++)
                  System.out.println("*");






3.5

       for(int i=3; i>0 ; i++)
               System.out.println("*");
                   



3.6

      for(int i=3; i>0 ; i--){
               System.out.println("*");
    }     
 




3.7


     for(int i=3; i>=0 ; i--){
        System.out.println("*");
        System.out.println("*");
    }





3.8

      for(int i=3; i>=0 ; i--) ;
             System.out.println("*");



3.9

      for(int i=3; i>=0; i--){
              for(int j=2; j>=0 ; j--){
                     System.out.print("*");
              }
      }




3.10


         for(int i=5; i >= 0 ; i--)
              for(int i=0  ; i<=5 ; i++)
                     System.out.print("*");




3.11

         for(int i=5, j=0 ; i>=0 ; i--){
              for(  ;  j>5; j--)
                        Sysetm.out.println("*");
         }



-------------------------------------------------------------------------------------------




4. Draw the pattern


  4.1

            for(int i=0;








2015年3月10日 星期二

java 練習一


1.

int a=10;
System.out.print(a++);
int b=a++;
System.out.println(++a);
System.out.println(b--);
System.out.println(-(--b));


--------------------------------------------------------------------

2.


int a = 5;
int b = a%=2;
System.out.println(-(b*=++b));
int c = a/=0;    // be carefully
System.out.println(c);





--------------------------------------------------------------------

Print the pattern:



3.1

int n=5;

for(int i=0; i<n ; i++){
    System.out.println("*");
}



3.2

int n=5;

for(int i=0; i<n; i++){
    System.out.print("x");
}



--------------------------------------------------------------------


4.


for( int i=0; i<3 ; i++){
    System.out.print("* ");
    System.out.print("* ");
}

System.out.println();

for( int i=0; i<3 ; i++)
     System.out.print("* ");
     System.out.print("* ");


--------------------------------------------------------------------


5.  Ans how many star will be created in following questions:


5.1)

     for(int i=0; i<5 ; i++)
        System.out.print("*");


5.2)

    for(int i=0; i<=5 ; i++)  {     
         System.out.print("*");
    }

5.3)


    for(int i=1; i<5 ; i++)    
         System.out.print("*");
         System.out.print("*");

5.4)

   for(int i=1; i<=10 ; i++)    
          System.out.print("*");
        

5.5)

    for(int i=0; i<10 ; i++);     
          System.out.print("*");
          System.out.print("*");


5.6)

     for(int i=0; i<5; i++){
          System.out.print("*");
     }
    
     for(int i=0; i<=4; i++){
          System.out.print("*");
     }


5.7)


      for(int i=0; i<3 ; i++){

           for(int j=0; j<10; j++){
                   System.out.println("*");
           }
         
     }     



5.8)


      
      for(int i=0; i<3 ; i++){

           for(int j=0; j<10; j++){
                   System.out.print("*");
           }

           System.out.println("*");
     }   




5.9)

      for(int i=0; i<3 ; i++){

           for(int j=0; j<10; j++){
                   System.out.print("*");
           }

           System.out.println(j);
     }     

   

--------------------------------------------------------------------


6.   Think and draw what patterns will be displayed in each programs.



6.1)


  for (int i=0; i<5; i++){
           for(int j=0; j<=i ;j++){
                  System.out.print("*");
           }
           System.out.print("\n");
  }




6.2)

      int n = 5;
      for (int i=1; i<=n; i++){
        
           for(int j=1; j<=n-i ; j++){
                  System.out.print("_ ");
           }
         
           for(int j=1; j<=i; j++){
                System.out.print("* ");
           }
         
           System.out.print("\n");

      }
  




       

lamj 筆記 1

1.

A: initialization
B: end condition
C: adjustment

  1. for ( A  ;  B  ;  C ) {
  2.            D
  3. }   

  4. E


次序 A -> B ->  true -> D -> C -> B -> true -> D -> C -> B -> false -> E;

 ------------------------------


2.

  1.  int a = 10;

  2.  System.out.println(a++);   // a = a + 1; 
  3.  System.out.println(a+3);  
  4.  System.out.println(a+=3);  // a = a + 3;
  5.  System.out.println(++a);    // a = 1+a;
  6.  System.out.println(a*2);   
  7.  System.out.println(a*=2);  // a = a * 2;
  8.  System.out.println(a/=a);  // a = a / a;
  9.  System.out.println(a%a);   
  10.  System.out.println((a));   
 


line    ans:
3.      10
4.      14
5.      14
6.      15
7.      30
8.      30
9.      1
10.    0
11.    1


Comment :

由於電腦read statement的方式是由左至右,所以以下兩句的output會有分別:
1. System.out.println(++a);
2. System.out.println(a++);

1 是prefix increment, 即先call ++ function ,後再println 變量 a 的值。
2 是postfix increment, 即先println 變量a的值,後再call  ++ function。



另外,

1. int a = 10;
2. int b = (a*=2);

line 2 的運作是想把一個"整數值"賦予給int類別的變量b ,而這個值需要由 (a*=2) 中拿出來 ,
但必須經過一輪運算 ,除非是 int b = a; ,你直接知道右邊的值。
而 a*=2; 中, 電腦看到 *= or /= or %= or += or -= or ++ or -- , 都會call 一個function 出來
去改變自身的值, 與 * or / or % or + 這些不同,不會改變自己的值;
所以, a*=2 會先做一個動作 a = a*2; 然後再彈出 a的值出來,即20。 此時,你就可以知道右邊的值是20了。
int b = (a*=2); 和 int b = a*=2; 是一樣意思