Java基礎

Java基礎

這裏寫圖片描述

第一天:Java概述和基本語法

  • Java歷史

    • 1995年5月23日 誕生
    • 1998年 Java 2
    • 2004年 Java 5
    • 2014年 目前的最新版本 Java 8
  • Java的特點

    • 簡單自然
    • 面向對象(Object-Oriented)
    • 可移植性(Write Once Run Anywhere)
    • 分佈式
    • 多線程
    • 安全健壯
  • Java的工作方式

    • 先編譯再解釋執行

說明:通過JDK提供的Java編譯器(javac)將Java源代碼(.java文件)編譯成類文件(.class文件,也叫做字節碼,這是一種人和計算機都無法識別的中間代碼,),再通過Java解釋器(java)啓動JVM加載類文件解釋執行。

  • Java的術語

    • JDK:Java Developer’s Kit - Java開發者工具
    • JRE:Java Runtime Environment - Java運行時環境
    • JVM:Java Virtual Machine - Java虛擬機
    • API:Application Programming Interface - 應用程序編程接口
  • 開發工具和開發環境

    • Eclipse
    • IntelliJ
  • Java程序的結構

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

說明:由於Java是面向對象的編程語言,Java程序通常是由類構成的,定義類的關鍵字是class,後面跟上類的名字,左花括號表示類的開始,右花括號表示類的結束。main方法是可執行程序的入口,它有三個修飾符,分別是:public、static和void,方法的開始和結束仍然是用花括號來界定的。方法中的代碼是語句,分號表示語句的結束。上面的程序使用了Java API中的System類的out對象的println方法在控制檯進行輸出。

練習1:輸出下面的圖案。

******************
*               *
*  歡迎來到朗沃  *
*               *
******************
package com.lovoinfo;

public class Hello {

    public static void main(String[] args) {
        System.out.println("*************************");
        System.out.println("*\t\t\t*");
        System.out.println("*\t歡迎來到朗沃\t*");
        System.out.println("*\t\t\t*");
        System.out.println("*************************");
    }
}

練習2:在彈出式對話框上輸出上面的圖案。

package com.lovoinfo;

import javax.swing.JOptionPane;

public class HelloGUI {

    public static void main(String[] args) {
        String name = JOptionPane.showInputDialog("請輸入你的名字: ");
        String message = "****************************\n"
                        + "\n*  歡迎" + name + "來到朗沃  *\n"
                        + "\n****************************";
        JOptionPane.showMessageDialog(null, message);
    }
}

練習3:兩個數做加減乘除的運算。

package com.lovoinfo;

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("請輸入第一個整數: ");
        int a = sc.nextInt();
        System.out.print("請輸入第二個整數: ");
        int b = sc.nextInt();
        System.out.printf("%d+%d=%d\n", a, b, a + b);
        System.out.printf("%d-%d=%d\n", a, b, a - b);
        System.out.printf("%d*%d=%d\n", a, b, a * b);
        System.out.printf("%d/%d=%d\n", a, b, a / b);
        sc.close();
    }
}

練習4:用彈出式對話框完成上面的程序。

package com.lovoinfo;

import javax.swing.JOptionPane;

public class CalculatorGUI {

    public static void main(String[] args) {
        String a = JOptionPane.showInputDialog("請輸入第一個數:");
        String b = JOptionPane.showInputDialog("請輸入第二個數:");
        int num1 = Integer.parseInt(a);
        int num2 = Integer.parseInt(b);
        String message = String.format(
                "%d+%d=%d", num1, num2, num1 + num2);
        JOptionPane.showMessageDialog(null, message);
    }
}

練習5:將英制單位的英寸轉換成公制單位的釐米(1英寸=2.54釐米)。

package com.lovoinfo;

import java.util.Scanner;

public class InchToCentimeter {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("請輸入英寸: ");
        double a = sc.nextDouble();
        double b = a * 2.54;
        System.out.printf("%.2f 英寸 = %.2f 釐米\n", a, b);
        sc.close();
    }
}

作業1:編程實現攝氏溫度轉換成華氏溫度。

package day0422;

import java.util.Scanner;

public class TempConverter {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        System.out.println("請輸入攝氏溫度:");
        double a =sc.nextDouble();
        double b =a/0.55556+32;
        System.out.printf("%.2f 攝氏溫度= %.2f 華氏溫度\n",a,b);
        sc.close();


    }

}

第二天:數據類型和常用運算

  1. 關鍵字、標識符、運算符、字面量和分隔符
  2. 數據類型
  3. 變量和常量
  4. 標識符的命名
  5. 運算符的使用

練習1:輸入兩個數找出其中較大的那個數。

package com.lovoinfo;

import java.util.Scanner;

public class FindMax {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("請輸入兩個數: ");
        int a = sc.nextInt();
        int b = sc.nextInt();
        if(a >= b) {
            System.out.println(a);
        }
        else {
            System.out.println(b);
        }
        sc.close();
    }
}

練習2:輸入身高(cm)和體重(kg)判斷身材是否正常。判斷標準"身高-110>=體重"認爲是正常的。

package com.lovoinfo;

import java.util.Scanner;

public class AreYouFat {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("請輸入你的名字: ");
        String name = sc.nextLine();
        System.out.print("請輸入你的身高: ");
        int height = sc.nextInt();
        System.out.print("請輸入你的體重: ");
        int weight = sc.nextInt();
        if(height - 110 >= weight) {
            System.out.println(name + "的身材正常!");
        }
        else {
            System.out.println(name + "是個胖子!");
        }
        sc.close();
    }
}

練習3:輸入一個年份,判斷是不是閏年。

package com.lovoinfo;

import java.util.Scanner;

public class IsLeapYear {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("請輸入一個年份: ");
        int year = sc.nextInt();
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            System.out.println(year + "是閏年");
        }
        else {
            System.out.println(year + "不是閏年");
        }
        sc.close();
    }
}

作業2:輸入三個整數,輸出其中最大的數。

package day0422;

import java.util.Scanner;

public class Big {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        System.out.println("請輸入三個數");
         int a = sc.nextInt();
         int b = sc.nextInt();
         int c = sc.nextInt();
         if(a>b&&a>c){System.out.println(a);}
         if(b>a&&b>c){System.out.println(b);}
         if(c>a&&c>b){System.out.println(c);}
         sc.close();
    }

}
發佈了21 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章