2013年7月5日 星期五

JAVA 第二章(共三十章)

第二章


 註解
  在輸入註解前,先輸入“//”(雙斜線),在這行中輸入的字都代表著「註解」(即不會被當做程式去編譯)。被稱為「end-of-line」或「single-line」。
  Traditional comments(又稱為multiple-line comments)。在註解開始時使用「/*」,註解結束時使用「*/」。
  ex. single-line://測試
  ex. multiple-line:
   /* 測試
     test
     end*/

 Class
  public class Welcome1{}
  「Welcome1」為使用者自訂的名稱,稱為Class Name。
  命名規則:不可用空格、底線、金錢符號。
  關於public與非public的宣告於第八章解釋。
  Class的主體(body)必需使用{}來涵蓋。

 Method
  public static void main ( String args[] ){}
  宣告Java Class時,通常會有一到多個方法。而其中一個method就必須如上所示,加上main(被當做程式的主體)。
  而void則表示著method會執行程式,但當程式執行完成時,任何訊息都不會被傳回。
  String args[]為一陣列,在第七章會提到。

  System.out.println( "Welcome to Java Programming!" );
  System.out是標準輸出物件(standard output object)。
  println是指印出後面的「Welcome to Java Programming!」後,將輸入游標往下一行(也就是按下Enter鍵,跳下一行)。
  後面帶著分號(;)代表著一個陳述句的結束。

  System.out.print( "Welcome to " );
  System.out.println( "Java Programming!" );
  print只有印出「Welcome to 」的部份,並未做其他動作。而在下一行的陳述句中表示接著印出「Java Programming!」,而在印完上一句的「Welcome to 」後,游標並未向下一行移動,而是在原地,所以會接著印。結果:「Welcome to Java Programming!」
  雖然分成兩個陳述句,印出的語句跟上面的例子一樣。

  System.out.println( "Welcome\nto\nJava\nProgramming!" );
  \n代表示「跳下一行」的意思,印出後會變成:
  Welcome
  to
  Java
  Programming!

  System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!" );
  printf的第一個參數是格式字串,裡面的兩個%s對應第二及第三個Fixed text。
  第一個%s(指後面的參數是字串)對應Welcome to、第二個對應Java Programming!。
  印出結果:
  Welcome to
  Java Programming!
  (←游標在這行)

  

復習(課本上原有解答)
  直接看課本:p.77~p.80

練習(自己寫的答案,並非正確解答)
 2.7 Write Java statements that accomplish each of the following tasks:
  a) Display the message "Enter an integer: ", leaving the cursor on the same line.
   System.out.printf("Enter an integer:");
  b) Assign the product of variables b and c to variable a.
   a=b*c
  c) State that a program performs a sample payroll calculation (i.e., use text that helps to document a program).
   //The program performs a sample payroll calculation.
 2.8 Assuming that x = 2 and y = 3, what dose each of the following statements display?
  a) System.out.printf("x = %d\n", x); x = 2
  b) System.out.printf("Value of %d + %d is %d\n", x, x, (x + x)); Value of  2 + 2 is 4
  c) System.out.printf("x="); x=
  d) System.out.printf("%d = %d\n", (x + y), (y + x)); 5 = 5
 2.9 Given that y = ax^3 + 7, which of the following are correct Java statements for this equation?
  a) y = a * x * x * x + 7;
  b) y = a * x * x * ( x + 7 );
  c) y = ( a * x ) * x * ( x + 7 );
  d) y = ( a * x ) * x * x + 7;
  e) y = a * ( x * x * x ) + 7;
  f) y = a * x * ( x * x + 7 );
   Ans: (a) (d) (e)
 2.10 Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following techniques:
  a) Use one System.out.println statement.
   System.out.println("1 2 3 4");
  b) Use four System.out.print statements.
   System.out.print("1 ");
   System.out.print("2 ");
   System.out.print("3 ");
   System.out.print("4\n");
  c) Use one System.out.printf statement.
   System.out.printf("%d %d %d %d\n", 1, 2, 3, 4);
 2.11 Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division). Use the techniques shown in Fig.2.7
  
  import java.util.Scanner;
  
  public class Addition{
   public static void main( String args[] ){
    Scanner input = new Scanner( System.in );

    int number1;
    int number2;

    System.out.print("Enter first integer: ");
    number1 = input.nextInt();

    System.out.print("Enter second integer: ");
    number2 = input.nextInt();

    System.out.printf ("\nSum is %d\n", (number1 + number2));
    //第一個"\n"用來分隔second integer與Sum
    System.out.printf ("Product is %d\n", (number1 * number2));
    System.out.printf ("Difference is %d\n", (number1 - number2));
    System.out.printf ("Quotient (Division) is %d\n", (number1 / number2));
   }
  }
 2.12
 
 
 
 2013/7/5 18:33 第一次編輯

沒有留言:

張貼留言