- int a = 10
(1)int:關鍵字。Java裏關鍵字共50個,可用48個。
(2) a:標識符。標識符可爲字母、下劃線、$、數字。
規定:
①數字不能放在開頭
②大小寫敏感
③不能與關鍵字衝突
④見名知意
⑤駝峯標識
⑥常量用全大寫表示,多個單詞用下劃線連接即可
(3)=:賦值運算符
(4)10: 字面 字符意思
①if、else) 條件運算
②ctrl+shift+o 組織導入
③== 等於
④!= 不等於
⑤&& 而且
⑥| | 或者
⑦/* */ 塊註釋
⑧ctrl+/ 行註釋
⑨ % 求模 (餘數)
⑩/t:製表符
⑪/n:換行
⑫ctrl+shift+f :格式化基本語言元素
- 關鍵字:程序中有特殊含義和通途的單詞
標識符:給變量、方法、類等起的名字
- 字母、數字、下劃線和$,數字不能開頭
- 大小寫敏感(區分大小寫)
- 不能跟關鍵字相同,不能包含特殊字符
- 見名知意,駝峯標識
運算符:指定某種運算的特殊符號
- 算術運算符:+、-、*、/、%
- 賦值運算符:=、+=、-=、*=、/=、%=、……
- 關係運算符:>、<、>=、<=、==、!=
- 短路運算符:&&、||
- 條件運算符:? :
- 自增自減運算符:++、–
- 類型轉換運算符:()
- 其他運算符:邏輯運算符、位運算符、移位運算符、下標運算符、成員運算符等
字面量:程序中不變的部分
- 引用型字面量:null
- 布爾型字面量:true和false
- 字符型字面量:‘q’,‘\n’,‘\t’,‘\ddd’[*]
- 整型字面量:29,035,0x1d
- 實型字面量:3.14,.25e2,5.5f
- 字符串字面量:“Hello, world”
- 類字面量:String.class,int.class
分隔符:空格、花括號、方括號、圓括號、分號、逗號、冒號等
數據類型
基本類型(primitive type)
- 整型:byte、short、int、long
- 實型:float、double
- 布爾型:boolean
- 字符型:char
枚舉類型(enumeration type):用於定義符號常量。
- 引用類型(reference type):除了基本數據類型和枚舉類型,剩下的類型都是引用類型。
變量和常量
- 變量:計算機語言中能儲存計算結果或能表示值抽象概念。變量可以通過變量名訪問。在指令式語言中,變量存儲的值通常是可變的,因此稱之爲變量。
- 常量:在程序運行時,不會被修改的量。Java中可以使用final關鍵字定義常量。
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);
}
*/
System.out.println(a >= b? a : 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 + "是個胖子!");
}
System.out.println(name +
(height - 100 >= weight? "身材正常!" : "是個胖子!"));
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();
}
}
練習4:輸入圓的半徑,計算圓的周長和麪積。
package com.lovoinfo;
import java.util.Scanner;
public class CalcCircle {
private static final double PI = 3.14;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入圓的半徑: ");
double radius = sc.nextDouble();
double area = PI * radius * radius;
double circumference = 2 * PI * radius;
System.out.println("周長: " + circumference);
System.out.println("面積: " + area);
sc.close();
}
}
練習5:輸入三個整數,按從小到大的順序輸出。
package com.lovo;
import java.util.Scanner;
public class SortThreeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入三個數: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a > b) {
int temp = a;
a = b;
b = temp;
}
if(a > c) {
int temp = a;
a = c;
c = temp;
}
if(b > c) {
int temp = b;
b = c;
c = temp;
}
System.out.printf("%d\t%d\t%d\n", a, b, c);
sc.close();
}
}
作業2:輸入三個整數,輸出其中最大的數。
import java.util.Scanner;
public class Roomwork {
public static void main(String[] args) {
Scanner scannern = new Scanner(System.in);
System.out.println("請輸入三個數");
int one = scannern.nextInt();
int two = scannern.nextInt();
int three = scannern.nextInt();
if(one>two && one>three){System.out.println(one);}
if(two>one && two>three){System.out.println(two);}
if(three>one && three>two){System.out.println(three);}
scannern.close();
}
}