Java周总结3
撰寫第三周課程總結及實驗報告(一)
Java實驗報告
?
班級 ? 計科一班 ? ? ? ? 學號 ? 20188375 ? ? ? 姓名? 湯云云 ? ??
完成時間 ? ? ? ? ? ?
評分等級????????????
實驗一 Java開發環境與簡單Java程序
一、?實驗目的
(1)?熟悉JDK開發環境
(2)?熟練掌握結構化程序設計方法
二、?實驗內容
三、?實驗過程
| 1. 打印輸出所有的“水仙花數”,所謂“水仙花數”是指一個3位數,其中各位數字立方和等于該數本身。例如,153是一個“水仙花數”。 |
| 實驗源碼: ? public class Flower {public static void main(String[] args){int n, g, s, b;for(n=100;n<1000;n++){g=n%10;s=n/10%10;b=n/100%10;if(n==g*g*g+s*s*s+b*b*b){System.out.println(n);}}} }? ? |
| 實驗結果:
|
?
| 2. 編寫Java程序,求13-23+33-43+…+973-983+993-1003的值。 |
| 實驗源碼: ? public class Sum {public static void main(String[] args){int n;int sum=0;for(n=1;n<=100;n++){if(n%2==0){sum=sum-(n*10+3);}else{sum=sum+n*10+3;}}System.out.println(sum);} }? |
| 實驗結果:
? |
?
| 3. 編程求1!+2!+3!+…+20!。 |
| 實驗源碼: public class Factorial {public static void main(String[] args){long sum=0;for(int i=1;i<=20;i++){sum+=fact(i);}System.out.println(sum);}public static long fact(int n){long product=1;for(int i=1;i<=n;i++){product=product*i;}return product;} }?
? ? |
| 實驗結果:
? |
?
| 4. 編寫Java程序,計算8+88+888+…前10項之和。 |
| 實驗源碼: ? public class Eight {public static void main(String[] args){int i;long item=8;long sum=0;for(i=1;i<11;i++){sum=sum+item;item=item*10+8;}System.out.println(sum);} }? |
| 實驗結果:
|
?
| 5. 一個數如果恰好等于它的因子之和,這個數就稱為完數。編寫程序輸出1000以內的所有完數。 |
| 實驗源碼: ? 1 public class WanShu 2 { 3 public static void main(String[] args) 4 { 5 int i,j; 6 for(i=1;i<=1000;i++) 7 { 8 int sum=0; 9 for(j=1; j<i; j++) 10 { 11 if(i%j==0) 12 { 13 sum=sum+j; 14 } 15 } 16 if(i==sum) 17 { 18 System.out.println( i ); 19 } 20 } 21 } 22 }? ? ? |
| 實驗結果:
? ? ? ? |
?
| 6. 編寫應用程序,輸出滿足1+2+3+…+n<8888的最大正整數。 |
| 實驗源碼: ? public class Bign {public static void main(String[] args){int i;int sum=0;for(i=1; sum+i<8888; i++){sum=sum+i;}System.out.println( i -1);} }? ? ? |
| 實驗結果:
? ? ? ? |
?
| 7. 使用for循環打印下面的圖形。
? |
| 實驗源碼: ? 1 public class Triangle 2 { 3 public static void main(String[] args) 4 { 5 for (int i = 0; i < 5; i++) 6 { 7 for (int j = i + 1; j < 5; j++) 8 { 9 System.out.print(" "); 10 } 11 for (int k = 0; k <= i; k++) 12 { 13 System.out.print("* "); 14 } 15 System.out.println(); 16 } 17 } 18 }? ? |
| 實驗結果:
? ? ? ? |
?
總結:
一、This 關鍵字的使用方法:
(1)強調調用本類方法
(2)表示類中的屬性
(3)可以使用this調用本類的構造方法
(4)this表示當前對象
以后在類中訪問屬性的時候都要加上this關鍵字
二、使用this調用構造方法的注意事項:
(1)this( )調用構造方法必須也只能放在構造方法的第一行
(2)對于this調用構造方法的時候一定要留一個構造方法作為出口
三、this表示當前對象:
在java中當前對象就是指當前正在調用類中方法的對象
四、static使用方法:
一個類中的方法不想由對象,而是由類名稱直接調用;
非static聲明的方法可以去調用static聲明的屬性或方法的。但是static聲明的方法是不能調用非static類型聲明的屬性或方法的。
?
例如:
?
class Person {private static String country="A城";private String name = "Hello";public static void sFun(String c){System.out.println("name="+name);fun( );}public void fun( ){System.out.println("World");} }?
?
?
?五、理解main( )方法
(1)public:表示此方法可以被外部所調用
(2)static:表示此方法可以由類名稱直接調用
(3)void:主方法是程序的起點,所以不需要任何的返回值
(4)main:系統規定好默認調用的方法名稱,執行的時候,默認找到main( )方法名稱
(5)String args[]:表示的是運行時的參數。參數傳遞的形式為“Java”類名稱? 參數1? 參數2 ? 參數3........"。
main()的定義:
public static void main(String args[])
?
轉載于:https://www.cnblogs.com/TheMatrixOfTYY/p/11493350.html
總結
- 上一篇: Java周总结1
- 下一篇: Java设计模式汇总