1. 程式人生 > >Java第四周學習總結

Java第四周學習總結

教材學習內容總結

1.運算子及表示式

算數運算子: 二目運算子:+  -  *  /  %

                       單目運算子:++  --

精度(由低到高):byte   short  int  long  float  double  (看第二週總結)、

關係運算符:>  <  >=  <=  ==  !=

邏輯運算子:&&(邏輯與) ||(邏輯或)  !(邏輯非)

賦值運算子:=(左邊必須為變數)

instanceof運算子:obj instanceof class 當物件是類或子類建立時,返回true

 

2.語句

2.1 if條件分支語句

if(條件){

if語句執行體

}

條件:必須是boolean型別 1==1 true||false

執行體:if條件為真,執行大括號中的執行體

為假 什麼也不做 跳過

注意 無 ; 符號

2.2 if-else語句

if(條件1){
//程式碼塊1
}else if(條件2){
//程式碼塊2
}else{//程式碼塊3}

2.3 switch語句

switch (表示式){
case 值1 : 語句1
break; 
case 值2 : 語句2
break; 
... 
default : 語句n
break; 
}

2.4 迴圈語句

2.4.1 for迴圈語句

public class forDemo {
    public static void main(String[] args){
        for (int x=0;x<20;x++){
            System.out.println("value of x:"+x);
        }
    }
}

2.4.2 while 迴圈語句

public class whileDemo {
    public static void main(String[] arys){
        int x = 10;
        while (x<20){
            System.out.println("value of x:" + x);
            x++;
        }
    }
}

2.4.3 do-while迴圈語句

public class do-whileDemo {
    public static void main(String[] arys){
        int x = 10;
        do{
            System.out.println("value of x:" + x);
            x++;
        }
        while (x<20);
    }
}

 

3.break與continue語句

        迴圈中break之後的語句都將被跳過,並且迴圈的執行也將終止,而轉去執行迴圈之後的其他語句。如果在一組巢狀迴圈中執行break語句,僅會退出break語句的最內層迴圈;continue語句與break語句類似,但它不會使迴圈結束。執行continue語句時,迴圈會跳出該語句之後直到迴圈結尾處的所有語句。否則,迴圈將和平常一樣執行。continue通常用來根據某個條件繞過迴圈中的一組語句,否則,迴圈會繼續執行。

 

4.編寫原始碼

學習Scanner類的基本使用,敲打有關程式碼。

原始碼:

import java.util.*;
public class  Sum {
	public static void main(String args[]) {
		Scanner scanner=new Scanner(System.in);
		System.out.println("請輸入兩個整數:");
		int a,b;
		a=scanner.nextInt();
		b=scanner.nextInt();
		int sum=a+b;
		System.out.println("兩數之和為:"+sum);
		
		Sqrt() ;
		Area();
	}
	
	public static void Sqrt() {
		Scanner scanner=new Scanner(System.in);
		System.out.println("請輸入一元二次方程的引數:");
		int a,b,c;
		double x1,x2;
		//do {
		a=scanner.nextInt();
		b=scanner.nextInt();
		c=scanner.nextInt();
		System.out.println("該方程為:"+a+"x*"+"+"+b+"x"+"+"+c+"=0");
		if ((b*b-4*a*c)<0) {
			System.out.println("該方程無實數根");
		}
		else {
			x1=(((-b)+Math.sqrt((b*b)-(4*a*c)))/(2*a));
			x2=(((-b)-Math.sqrt((b*b)-(4*a*c)))/(2*a));
			if (x1==x2) {
				System.out.println("該方程的根為:"+x1);
			}
			else
			System.out.println("該方程的根分別為:"+x1+"和"+x2);
			}
			//}}while((b*b-4*a*c)<0);
		
	}
	
	public static void Area() {
		Scanner scanner=new Scanner(System.in);
		System.out.println("請輸入三角形的三條邊:");
		int a,b,c,p;
		a=scanner.nextInt();
		b=scanner.nextInt();
		c=scanner.nextInt();
		p=(a+b+c)/2;
		double S;
		S=Math.sqrt(p*(p-a)*(p-b)*(p-c));
		System.out.println("該三角形的面積為:"+S);
	}

}

 

 

教材學習中的問題與解決過程

無..

 

程式碼除錯中的問題與解決過程

問題:不知道應該引用util具體哪個包

解決:import java.util.*;......

問題:定義了三個主函式(辣雞)

解決:改為定義一個主函式,引用其餘兩個函式

 

 

學習進度條

  程式碼行數(新增/累積) 部落格量(新增/累積) 學習時間(新增/累積) 重要成長
目標 5000行 30篇 400小時  
第四周 96/149 1/3 8/22 基本語句的複習
第二週 35/53 1/2 6/14

資料型別應用

第一週 18/18 1/1 8/8 eclipse的使用