国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java:基本語法

2019-11-15 00:32:44
字體:
來源:轉載
供稿:網友
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 }

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

類型變量初始值
字節型 byte0
整型 int0
單精度型 fload0.0f
字符型 char'/u0000'
字符串型 Stringnull
短整型 short0
長整型 long0L
雙精度型 double0.0d
布爾型 booleanfalse

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.

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();        }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浦县| 浠水县| 五华县| 通许县| 德兴市| 龙江县| 梧州市| 内江市| 古浪县| 玉田县| 新巴尔虎右旗| 金阳县| 定边县| 屏山县| 巩留县| 曲水县| 称多县| 登封市| 宝山区| 泸西县| 钟山县| 平南县| 赤城县| 南雄市| 文水县| 岳阳县| 延边| 旌德县| 石嘴山市| 富源县| 仁布县| 宣城市| 长白| 连州市| 绥化市| 平山县| 广西| 宜阳县| 高淳县| 汤阴县| 辽阳县|