標籤:post 北京 wce 學生 java char bre limit 傳回值
第二章
1. 資料類型 變數名 =值;
Eg:
Double score =15.65;
String name =“張三”;
Char sex =“男”;
- 資料類型強轉
資料類型 變數名 =(資料類型)值;
樣本:
Double num=15.75;
Int sum =(int)num;
結果:sum=15;
第三章1.if選擇結構
文法: if(條件){
代碼塊 //條件成立後要執行的代碼,可以是一條語句,也可以是一組語句
}
樣本:
If(7>5){
System.out.println(“true”);
}
- if——else選擇結構
文法:if(條件){
//代碼塊1
}else{
//代碼塊2
}
樣本:
if(chengji>98){
System.out.println("老師說:不錯,獎勵一個mp4");
}else {
System.out.println("老師說:進行編碼懲罰!!!");
}
3.多重if選擇結構
文法:if(條件1){
//代碼塊1
}else if(條件2){
//代碼塊2
}else{
//代碼塊3
}
樣本:
if(qian>=500){
System.out.println("太好了,我可以買一輛凱迪拉克");
}else if (qian>=100){
System.out.println("還可以,我可以賣一輛帕薩特");
}else if (qian>=10){
System.out.println("我可以買一輛奧拓");
}else if (qian>=5){
System.out.println("我可以買一輛伊蘭特");
}else{
System.out.println("看來我自能買一輛捷安特了");
}
4.嵌套if選擇結構
文法:
if(條件1){
if(條件2){
//代碼塊1
}else{
//代碼塊2
}
}else{
//代碼塊3
}
樣本:
if(chengji<=10){
if(xingbie.equals("男")){
System.out.println("進入男子組決賽");
}else if(xingbie.equals("女")){
System.out.println("進入女子組決賽");
}
}else{
System.out.println("淘汰!!!!");
}
第四章1. switch選擇結構
文法:switch(條件){
case 常量 1:
//代碼塊1;
break;
case 常量 2:
//代碼塊2;
break;
......
default:
//代碼塊n;
break;
}
樣本:
switch(mingci){
case 1:
System.out.println("參加麻省理工大學組織的一個月夏令營");
break;
case 2:
System.out.println("獎勵惠普膝上型電腦一部");
break;
case 3:
System.out.println("獎勵移動硬碟一個");
break;
default:
System.out.println("沒有任何獎勵");
break;
}
第五章1. while迴圈結構
文法:while(迴圈條件){
//迴圈操作
}
樣本:
int i =1;
while(i<=100){
System.out.println("努力奮鬥”);
i = i + 1;
}
2. do-while迴圈
do{
//迴圈操作
}while(迴圈操作);
樣本:
int i = 1;
do{
System.out.println("努力奮鬥!")
i++;
}while(i<=100);
第六章1. for迴圈
文法:
for(運算式1;運算式2;運算式3){
//迴圈體
}
樣本:
for(i=0, j=num;i<=num;i++,j--){
System.out.println(i+"+"+j+"="+(i+j));
}
- break的使用
樣本:
for(int i =0;i<5;i++){
System.out.println("請輸入第" + (i + 1) + "門成績:");
score = input.nextInt();
if(score<0){
isNegative = true;
break;
}
- continue的使用
樣本:
for(int i = 0;i<totle;i++){
System.out.println("請輸入第"+(i+1)+"位學生的成績:");
score =input.nextInt();
if(score<80){
continue;
}
第八章
1.聲明數組
文法:
資料類型[]數組名; 或者 資料 數組名[];
樣本:
int[] scres;
2,分配空間
文法:
數組名 = new 資料類型[數組長度];
樣本:
scores = new int[10];
綜合方法:
資料類型[] 數組名 = new 資料類型[數組長度];
樣本:
int scores[] = new int[10];//儲存10個資料得分
注意!一旦聲明了數組的大小就不能修改.即數組的長度是固定的,
3.賦值
分配空間之後就可以向數組裡存放資料了,數組中每一個元素都是通過下標來訪問的
文法:
數組名[下標值];
樣本:
向scores數組中存放資料
scores[0] = 10;
文法:
資料類型[] 數組名={值1,值2,值3,.........值n};
樣本:
int [] scores = {30.20.10,60.78};
也可以寫為:
int[] scores = new int[]{30,20,10,60,78};
4.數組排序
文法:Arrays.sort(數組名);
- 求數組最大值
樣本:
max = scores[0];
for(int i = 1;i<scores.length;i++){
if(scores[i]>max){
max = scores[i];
}
- 插入元素
樣本:
for(int i =0;i<list.length;i++){
if(num>list[i]){
index = i;
break;
}
}
//元素後移
for(int j = list.length-1;j>index;j--){
list[j]= list[j-1];//index下標開始的元素後移一個位置
第九章1. 二重迴圈結構
1.文法://while與while迴圈嵌套
while(迴圈條件1){
//迴圈操作1
while(迴圈條件2){
迴圈操作2
}
}
樣本:
Int i=0;
Int j=0;
While(i<3){
While(j<3){
System.out.println("努力奮鬥!");
J++;
}
I++;
}
2.//do-while與do-while迴圈嵌套
do{
//迴圈操作1
do{
//迴圈操作2
}while(迴圈條件2);
}while(迴圈條件1)
樣本:
Int i=0;
Int j=0
Do{
Do{
System.out.println("努力奮鬥!");
J++;
}while(j<3)
I++;
}while(i<3)
3. //for與for迴圈嵌套
for(迴圈條件1){
//迴圈操作1
for(迴圈條件2){
//迴圈操作2
}
}
樣本:
for (int i = 0; i < average.length; i++) {
sum=0.0;
System.out.println("請輸入第"+(i+1)+"個班級的成績");
for (int j = 0; j < score.length; j++) {
System.out.println("第"+(j+1)+"個學員的成績");
score[j]=input.nextInt();
sum+=score[j];
}
average[i]=sum/score.length;
System.out.println("第"+(i+1)+"個班級參賽學院的平均分是"+average[i]+"\n");
}
4.//while與for迴圈嵌套
while(迴圈條件1){
//迴圈操作1
for(迴圈條件2){
//迴圈操作2
}
}
樣本:
for(int i=1;i<num;i++{
while(b[0]!=a[i]) {
b[1]=a[i];
count++;
break;
find = true;
}
if (find)
break;
}
第十一章1. Java的類模板
文法:
public class <類名> {
}
樣本:
public class School {
String schoolName;
int classNumber;
int labNumber;
- 方法聲明
文法:
存取修飾詞 傳回值類型 方法名( ) {
//方法體
}
樣本:
public void showCenter( ){
System.out.println( schoolName+"培訓中心\n "+"配備:"+classNumber+"教室"+labNumber+" 機");
}
3. 如何建立和使用對象
建立對象的文法:
類名 對象名 = new 類名( );
樣本:
School center = new School( );
對象名 . 屬性 //引用對象的屬性
對象名 . 方法名 //引用對象的方法
樣本:
center.name = " 北京中心"; //給name 屬性賦值
center.showCenter( ); //調用showCenter( )方法
第十二章
1.如何定義類的方法
文法:
public 傳回值類型 方法名(){
//方法的主體
}
文法:
return 運算式;
樣本:
public String robBall{
String ball =“一”;
return ball;
第十四章
1.定義帶參方法
文法:<存取修飾詞>傳回值類型<方法名>(<參數列表>){
//方法主體
}
樣本:public void show(String name,int age){
}
第十五章
String name="張三";
1.擷取字串的長度
文法:length();
樣本:
name.length();
2.比較字串
文法: equals();
樣本:
name.rquals("張三")
3.連接字串
文法: concat();
樣本:
name.concat("18歲");
4.提取字串
文法:substring();
樣本:
name.substring(0); //提取第0位
5.搜尋字串
文法:indexOf();
樣本:
name.indexOf(1); //返回第1位的值
6.拆分字串
文法:split(String separator,int limit);
樣本:
String ci ="長亭外 古道邊";
chai =ci.split(" "); //按空格拆分
7.去除首尾空格
文法:trim();
樣本:
name.trim();
StringBuffer
8.轉換成String類型
文法:toString();
樣本:
StringBuffer a =new StringBuffer("abcdefg");
a.toString();
9.連接字串
文法:append();
樣本:
StringBuffer a =new StringBuffer("abcdefg");
a.append("hijk");
10.插入字串
文法:insert();
樣本:
StringBuffer a =new StringBuffer("abcdefg");
for(int i=a.length()-3;i>0;i=i-3){
a.insert(i,‘,‘);
}
《java文法執行個體2~15章》