一。開發環境
用eclipse開發。步驟如下:
創建project->src下創建package->package下創建class;
二。變量和常量
1.關鍵字、標識符不可命名
2.數據類型
String-->字符串
3.數據類型轉換
自動:高精度向低精度丟失信息。
強制:(數據類型)
4.注釋
單行注釋:
//...
多行注釋:
/* ...
* ...
*/
文檔注釋:
/** ...
* ...
*/
三。運算符
1.算數運算符
2.賦值運算符
3.比較運算符
4.邏輯運算符
5.條件運算符
(boolean)?(true):(false)
6.位運算符
|或
&與
^異或
四。流程控制語句
1.條件
1.1條件之if
A:
if(...)
...;
B:
if(...)
...;
else if(...)
...;
else
...;
1.2.條件之switch
switch ...{ //...為int或char
case ...:
...;
break;
case ...:
case ...:
...;
break;
defult:
...;
}
2.循環
2.1循環之while
while(...)
{
...;
}
2.2循環之do-while
do
{
...;
}while(...);
2.3循環之for
for(...;...;...)
{
...;
}
3.跳轉
break;
continue;
五。數組
1.定義方式
(a).
int[] score; //聲明
//int score[];
score = new int[10]; //分配空間
(b).
int[] score = new int[10]; //聲明,分配空間
(c).
int[] score = {...}; //聲明,分配空間,賦值
int[] score = new int[]{...}; //聲明,分配空間,賦值
2.賦值方式
scores[i]=x;
3.迭代
for(int i=0;i<scores.length;i++);
for(int i:scores);
4.Arrays類
import java.util.Arrays; //導入Arrays
(a).排序
Arrays.sort(數組名);
(b).數組轉換成字符
該方法按順序把多個數組元素連接在一起,多個元素之間使用逗號和空格隔開
Arrays.toString(數組名);
5.二維數組
類似一維數組,將[]變為[][]即可。
六。方法
1.格式
訪問修飾符 返回類型 方法名(參數列表)
{
方法體
}
2.方法重載
同一個類中,方法名相同,方法的參數類型,個數,順序不同。
3.注意
必須在主函數創建對象,之后才能調用方法。
七。輸入輸出
1.輸出
System.out.println(); //換行
System.out.print(); //不換行
快捷鍵:
syso-->Alt+/ //System.out.println();
main-->Alt+/ //public static void main(String[] args) {}
2.輸入
(a).導入包java.util.Scanner,在package下面
import java.util.Scanner;
(b).創建Scanner對象,main函數里面
Scanner input=new Scanner(System.in);
(c).輸入
int score=input.nextInt();
八。編程例
實現輸入數n,表示總成績數目,接着輸入n個整數,表示成績,對成績排序,並輸出有效的前三名。(有效是指在0-100內,包括0和100)

import java.util.Arrays; import java.util.Scanner; public class HelloWorld { //完成 main 方法 public static void main(String[] args) { Scanner input=new Scanner(System.in); HelloWorld hello=new HelloWorld(); int scores[]=new int[100]; int n=input.nextInt(); for(int i=0;i<n;i++) { scores[i]=input.nextInt(); } hello.prsort(scores); } //定義方法完成成績排序並輸出前三名的功能 public void prsort(int[] scores) { Arrays.sort(scores); int k=0; for(int i=scores.length-1;i>=0&&k<3;i--) { if(scores[i]>100||scores[i]<0) continue; else { System.out.println(scores[i]); k++; } } } }