1. 程式人生 > >《java語法實例2~15章》

《java語法實例2~15章》

tin 連接 string類型 創建對象 indexof() 獲取字符串 out bstr 參數

第二章

1. 數據類型 變量名 =值;

Eg:

Double score =15.65;

String name =“張三”;

Char sex =“男”;

  1. 數據類型強轉

數據類型 變量名 =(數據類型)值;

示例:

Double num=15.75;

Int sum =(int)num;

結果:sum=15;

第三章

1.if選擇結構

語法: if(條件){

代碼塊 //條件成立後要執行的代碼,可以是一條語句,也可以是一組語句

 }

示例:

If(7>5){

System.out.println(“true”);

}

  1. 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));

}

  1. break的使用

示例:

for(int i =0;i<5;i++){

System.out.println("請輸入第" + (i + 1) + "門成績:");

score = input.nextInt();

if(score<0){

isNegative = true;

break;

}

  1. 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(數組名);

  1. 求數組最大值

示例:

max = scores[0];
for(int i = 1;i<scores.length;i++){
if(scores[i]>max){
max = scores[i];
}

  1. 插入元素

示例:

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;

  1. 方法聲明

語法:

訪問修飾符 返回值類型 方法名( ) {

  //方法體

}

示例:

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章》