Java:基本語法


Java語言是由類和對象組成的,其對象和類又是由變量和方法組成,而方法,又包含了語句和表達式。

 

1. 變量

Java語言提供了兩種變量:成員變量和局部變量

  • 成員變量:是在方法體外的類中聲明和定義的,可以被自動初始化
  • 局部變量:是在方法中聲明和定義的,不能被自動初始化,方法執行完,局部變量也就不存在了

在Java中,使用任何變量之前都需要對變量進行創建,創建變量實際上就是對變量的聲明過程,需要指明變量類型和變量名。

1 int a;
2 boolean b;
3 char c = 'A';
 1 public class DataDemo {
 2     
 3     int a;
 4     public void test()
 5     {
 6         boolean b=false;
 7         char c='\0';
 8     }
 9 
10     public static void main(String[] args) {
11         float f=0;
12         String s=null;
13 
14     }
15 
16 }

成員變量對應的自動初始化值:

類型變量 初始值
字節型 byte 0
整型 int 0
單精度型 fload 0.0f
字符型 char '\u0000'
字符串型 String null
短整型 short 0
長整型 long 0L
雙精度型 double 0.0d
布爾型 boolean false

 

2. 4類基本數據類型

Java數據類型:

  • 布爾型
  • 整型: 整型,短整型,長整型,字節型
  • 字符型
  • 浮點型:單精度型,雙精度型

String不是基本數據類型。String類所定義的變量是一個對象,而不是簡單類型。與簡單類型不同,類的對象含有自己的方法,是復雜類型。

布爾型(boolean),用於邏輯條件判斷,只含兩個值,真(true)、假(false)。需要注意的是,在C語言中,1和true等價,0和false等價,但在Java中,boolean變量的取值只可能是true或false。

 

3. 算數運算符

取模運算(%)中,若操作數包含正負數,則結果的正負號與左操作數一致。 例如: -8%3=-2, 8%(-3)=2

 

4. switch語句

public class SwitchTest {

    public static void main(String[] args) {
        int student[] = {95, 85, 75, 65, 55};
        for(int i=0; i<5; i++)
        {
            switch(student[i]/10)
            {
                case 9:
                    System.out.println("Student" + i + "'s result is A!");
                    break;
                case 8:
                    System.out.println("Student" + i + "'s result is B!");
                    break;
                case 7:
                    System.out.println("Student" + i + "'s result is C!");
                    break;
                case 6:
                    System.out.println("Student" + i + "'s result is D!");
                    break;
                default:
                    System.out.println("Student" + i + "'s result is F!");
                    
            }
                
        }

    }

}

Student0's result is A!
Student1's result is B!
Student2's result is C!
Student3's result is D!
Student4's result is F!

public class SwitchTest {

    public static void main(String[] args) {
        int student[] = {95, 85, 75, 65, 55};
        for(int i=0; i<5; i++)
        {
            switch(student[i]/10)
            {
                case 9:
                    System.out.println("Student" + i + "'s result is A!");
        
                case 8:
                    System.out.println("Student" + i + "'s result is B!");
                    
                case 7:
                    System.out.println("Student" + i + "'s result is C!");
                    
                case 6:
                    System.out.println("Student" + i + "'s result is D!");
                    
                default:
                    System.out.println("Student" + i + "'s result is F!");
                    
            }
                
        }

    }

}

Student0's result is A!
Student0's result is B!
Student0's result is C!
Student0's result is D!
Student0's result is F!
Student1's result is B!
Student1's result is C!
Student1's result is D!
Student1's result is F!
Student2's result is C!
Student2's result is D!
Student2's result is F!
Student3's result is D!
Student3's result is F!
Student4's result is F!

注意:當已經進入一個case分支,同時這個case語句中並沒有使用break,那么以后的每個case都不用匹配就可以直接進入,知道遇到break為止。

 

5. 實戰練習

  • 使用for循環來實現對1~99之間奇數的求和
    import javax.swing.JOptionPane;
    
    
    public class OddSum {
    
        public static void main(String[] args) {
            int sum=0;
            for(int i=1; i<=99; i++)
            {
                if(i%2!=0)
                {
                    sum=sum+i;
                }
            }
            JOptionPane.showMessageDialog(null, "The sum is "+ sum, "Total Even Integer from 1 to 99", JOptionPane.INFORMATION_MESSAGE);
    
        }
    
    }

 

  • switch語句與for循環結合
    public class TestSwitch
    {
        int i=0, w=0;
        public TestSwitch()
        {
            for(; i<=5; i++)
            {
                switch (i)
                {
                case 3: w+=1;
                case 0: w+=1;
                case 1: w+=1; continue;
                case 2: w+=1;
                case 4: w+=1;
                default:
                    w+=2;
                }
                System.out.print(" "+w);
            }
            
        }
    
        public static void main(String[] args) 
        {
            TestSwitch testSwitch=new TestSwitch();
        }
    
    }

     7 13 15

  • 利用多重for循環,用“*”繪制一個直角三角形,並使用消息對話框顯示出來
import javax.swing.JOptionPane;


public class MultipleLoop1 {

    public static void main(String[] args) {
        String out="";
        loop:
            for(int row=1; row<=5; row++)
            {
                out+="\n";
                for(int column=1; column<+6; column++)
                {
                    if(column>row) continue loop;
                    out+="*  ";
                }
            }
        JOptionPane.showMessageDialog(null, out, "Test multiply loop 1", JOptionPane.INFORMATION_MESSAGE);

    }

}

  • 用“*”輸出一個菱形並顯示
public class MultiplyLoop2 {

    public static void main(String[] args) {
        MultiplyLoop2 multiplyLoop2=new MultiplyLoop2();
        multiplyLoop2.print(11);

    }
    
    public void print(int n)
    {
        int temp=0;
        for(int i=0; i< n; i++)
        {
            for(int j=0; j<Math.abs(n/2-i);j++)
            {
                System.out.print(" ");
            }
            if(i<=n/2)
            {
                temp=i;
            }
            else
            {
                temp=n-i-1;
            }
            for(int k=0; k<(2*temp +1); k++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM