編碼練習——Java-1-基礎__Ajax

來源:互聯網
上載者:User

歡迎訪問 部落格新址 基礎練習 輸出

public class MyApp {    public static void main(String[] args) {        System.out.println("我能學好Java語言。");    }}
整數類型
public class Test {    public static void main(String[] args) {        byte mybyte = 124;        short myshort = 32564;        int myint = 45784612;        long mylong = 46789451L;        long result = mybyte+myshort+myint+mylong;        System.out.println("四種類型相加的結果為:"+result);    }}
浮點型
public class Test {    public static void main(String[] args) {        float f1 = 12.23f;        double d1 = 4562.12d;        double d2 = 45678.1564;        double result = f1+d1+d2;        System.out.println("浮點數相加達到結果為:"+result);    }}
字元型
public class Test {    public static void main(String[] args) {        int i = 'd';        char c = 97;        System.out.println("字元d的Unicode碼是:"+i);        System.out.println("Unicode碼97代表的字元是:"+c);    }}
逸出字元
public class Test {    public static void main(String[] args) {        char char1 = '\\';        char char2 = '\u2605';        System.out.println("輸出反斜線:" + char1);        System.out.println("輸出五角星:" + char2);    }}
變數(聲明與定義)
public class Test {    public static void main(String[] args) {        int i= 10;        System.out.println("i的初值是:"+i);        i= 100;        System.out.println("i現在的值是:"+i);    }}
聲明常量
final 一般大寫
public class Test {    public static void main(String[] args) {        final double PI = 3.1415926F; // final 大寫        final boolean BOOL = true;    }}
變數和常量(靜態變數)
public class Test {    static final double PI = 3.14;    static int age = 23;    public static void main(String[] args) {        final int number;        number = 1235;        age = 22;        System.out.println("常量PI的值為:"+PI);        System.out.println("賦值後number的值為:"+number);        System.out.println("int型變數age的值為:" + age);    }}
成員變數(靜態變數)和局部變數
public class Test {    static int times = 3;    public static void main(String[] args) {        int times = 4;        System.out.println("times的值為:" + times);        System.out.println("times的值為:" + Test.times);    }}
賦值運算子
public class Test {    public static void main(String[] args) {        int a,b,c;        a = 15;        c = b = a+4;        System.out.println("c的值為:" + c);        System.out.println("b的值為:" + b);    }}
算術運算子
    public static void main(String[] args) {        float number1 = 45.56f;        int number2 = 152;        System.out.println("45.56f和152的和為:" + (number1+number2));        System.out.println("45.56f和152的差為:" + (number1-number2));        System.out.println("45.56f和152的積為:" + (number1*number2));        System.out.println("45.56f和152的商為:" + (number1/number2));    }}
關係運算子
public class Test {    public static void main(String[] args) {        int number1 = 4;        int number2 = 5;        System.out.println("4>5是否成立:" + (number1>number2));        System.out.println("4<5是否成立:" + (number1<number2));        System.out.println("4==5是否成立:" + (number1==number2));        System.out.println("4!=5是否成立:" + (number1!=number2));        System.out.println("4>=5是否成立:" + (number1>=number2));        System.out.println("4<=5是否成立:" + (number1<=number2));    }}
邏輯運算子
public class Test {    public static void main(String[] args) {        int a = 2;        int b = 5;        boolean result1 = ((a>b) && (a!=b));        boolean result2 = ((a>b) ||(a!=b));        System.out.println("(a>b) && (a!=b)的值是:" + result1);        System.out.println("(a>b) ||(a!=b)的值是:" + result2);    }}
按位元運算符
public class Test {    public static void main(String[] args) {        int i = 46;        int j = 97;        char c = 'a';        System.out.println("46 & 97 的值是:" + (i&j));        System.out.println("46 & 'a' 的值是:" + (i^c));        System.out.println("46 >>1  的值是:" + (i>>1));    }}
三元條件運算子
public class Test {    public static void main(String[] args) {        int i = 46;        int j = 97;        int z = i>j ? 100 : 200;        System.out.println("i>j?100:200 的值是:" + z);    }}
執行個體 - 不用其它變數實現兩個變數互換
import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        System.out.println("請輸入變數A的值:\n");        long A = scan.nextLong();        System.out.println("請輸入變狼B的值:\n");        long B = scan.nextLong();        System.out.println("A="+A+"\tB="+B);        System.out.println("執行變數互換...");        A = A^B;        B = B^A;        A = A^B;        System.out.println("A="+A+"\tB="+B);    }}
執行個體 - 判斷數位奇偶性
import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        System.out.println("請輸入一個整數:");        long number = scan.nextLong();        String check = (number%2 == 0) ? "這個數字是:偶數" : "這個數字是:奇數";        System.out.println(check);    }}
自動類型轉換
public class Test {    public static void main(String[] args) {        float number1 = 45f;        int number2 = 152;        System.out.println(number1+number2);    }}
public class Test {    public static void main(String[] args) {        byte mybyte = 127;        int myint = 150;        float myfloat = 452.12f;        char mychar = 10;        double mydouble = 45.46546;        //將運算結果輸出        System.out.println("127 + 452.12 = " + (mybyte+myfloat));        System.out.println("127*150 = " + (mybyte*myint));        System.out.println("127/10 = " + (mybyte/mychar));        System.out.println("45.46546+10 = " + (mydouble+mychar));    }}
強制類型轉換
public class Test {    public static void main(String[] args) {        int a = (int) 45.23;        long y = (long)456.6F;        int b = (int)'a';        System.out.println("a = " + a);        System.out.println("y = " + y);        System.out.println("b = " + b);    }}
執行個體 - 類型轉換
public class Test {    public static void main(String[] args) {        int intNum = 4;        float floatNum = 9.5F;        floatNum /= intNum; //自動類型轉換        System.out.println("9.5F/4 = " + floatNum);        double numX = 4.88;        double numY = 78.83;        int numZ = (int)numX + (int)numY; //強制類型轉換        System.out.println("4.88 + 78.83 = " + numZ);        char charVar = 'T';        int intVar = (int) charVar;        System.out.println("將字元T轉換成int型變數是:" + intVar);        int num1 = 34;        double num2 = (double) num1/3;        System.out.println("34的三分之一是:"+num2);    }}
注釋
單行 多行 文檔注釋
public class Test {    public static void main(String[] args) {        // 單行注釋        /*         * 多行注釋1         * 多行注釋2         */        /**         * 文檔注釋         */    }}
執行個體 - 判斷某一年是否是閏年
import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        System.out.println("請輸入一個年份:");        long year = scan.nextLong();        if(year%4==0 && year%100!=0 || year%400==0) {            System.out.println(year + "年是閏年。");        }else {            System.out.println(year + "年不是閏年。");        }    }}
執行個體 - 求球形的體積
import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        System.out.println("請輸進球形的半徑:");        double r = scan.nextDouble();        final double PI = 3.1415926;        double volume = 4.0/3.0*PI*r*r*r;        System.out.println("球形的半徑是:" + r);        System.out.println("圓周率是:" + PI);        System.out.println("球形的體積是:" + volume);    }}
複合陳述式
public class Test {    public static void main(String[] args) {        int y = 40;        System.out.println("輸出y的值:" + y);        int z = 245;        boolean b;        // 複合陳述式        {            b = y>z;            System.out.println("y>z城裡嗎:" + b);        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.