表明語句: while , do while, for
java
while語句格式: while (條件表達式) { 執行語句; }
注:當在控制檯進入無限循環,按ctrl+c便可中止 小程序
do while語句格式: 數組
do { 執行語句; }while(條件表達式);
do while 特色是條件不管是否知足,循環體至少被執行一次. 微信
for (int x = 0; x<3;x++) { System.out.println("x="+x);//3 6 }
//在這裏離開了大括號,x不存在的,注意(條件表達式) System.out.println("x====="+x); int y = 0; while(y<3) { System.out.println("y="+y); y++; } System.out.println("y===="+y); /* 1.變量有本身的做用域,對於for來說:若是將用於控制循環的增量定義在for語句中,那麼該變量只在for語句內有效, 2,for和while 能夠進行呼喚,若是須要定義循環增量,用for更合適 總結: 何時試用循環結構? 當要對某些語句執行不少次時,就試用循環結構. */
class ForTest { public static void main(String[] arg) { int x = 1; for(System.out.println("a");x<3;System.out.println("c")) { System.out.println("d"); x++; } //abcdc } } ///爲了檢測語句是怎麼執行的,先來什麼後作什麼
/*1,獲取1~10的和,並打印. 2,1~100之間的7的倍數的個數,並打印.*/ class ForTest2 { public static void main(String[] arg) { //1,定義變量用於存儲不斷變化的和. int sum = 0; //2,定義變量,記錄住不斷變化的被加的數. int x =1; //3,定義循環,重複加法的過程, while(x<=10) { sum = sum + x; x++; } System.out.println("sum="+sum); /* 循環注意: 必定要明確哪些語句須要參與循環,哪些不須要. 0+1 1+2 3+3 6+4 */ } }
//用for來提現, int sum = 0; for(int x=0; x<=10;x++) { sum+=x; } System.outprintln("for sum = "+sum);
/* 2,1~100之間的7的倍數的個數,並打印. 思路: 1,先對1~100進行循環(遍歷)經過循環的形式 2,在遍歷的過程當中,定義條件,只對7的倍數進行操做. 3,由於7的倍數不肯定,只要符合條件,就經過一個變量來記錄這個變量的次數 步驟: 1,定義循環語句,選擇for語句. 2,在循環中定義判斷.只要是7的倍數便可,試用if語句.條件: 7的倍數x%7=0 3,定義變量,該變量隨着7的倍數的出現而自增. */ class ForTest3 { public static void main(String[] arg) { int count = 0; for(int x=1; x<=100;x++) { if(x%7==0) //System.out.println("x= "+x); count++; } System.out.println("count="+count); /* 計數器思想. 經過一個變量記錄住數據的狀態變化. 也須要經過循環完成. */ } }
//語句嵌套,其實就是語句中還有語句. //循環嵌套. class ForForDemo { public static void main(String[] arg) { for(int x = 0; x < 3; x++) { for(int y =0; y<4; y++) { System.out.print("*"); } System.out.println();//只有一個功能就是換行. } System.out.println("---------------"); /* ***** **** *** ** * 發現圖形有不少行,每個行有不少列, 要試用嵌套循環,原理:形象說法:大圈套小圈 */ for(int x=0;x<5;x++)//x<5由於外循環控制行數,一共5行. { for(int y=x;y<5;y++) { System.out.print("*"); } System.out.println(); } } } /* **** **** **** 對於打印長方形:外循環控制的行數,內循環控制的是每一行的列數,也就是一行中元素的個數 * ** *** **** ***** */
class ForForTest { public static void main(String[] arg) { /* * ** *** **** ***** 不是規律的規律 尖朝上,能夠改變條件,讓條件隨着外循環變化. 尖朝下,能夠初始化值,讓初始化隨着外循環變化. */ for (int x=0; x<=5;x++) { for(int y=0;y<=x; y++) { System.out.print("*"); } System.out.println(); } System.out.println("----------------"); /* 1 12 123 1234 12345 */ for(int x=1;x<=5;x++) { for(int y=1;y<=x;y++) { System.out.print(y); } System.out.println(); } } }
class ForForTest { public static void main(String[] arg) { 九九乘法表 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 */ for(int x=1;x<=9;x++) { for(int y = 1;y<=x;y++) { System.out.print(y+"*"+x+"="+y*x+"\t"); } System.out.println(); } } }
break(跳出) continue(繼續) 函數
break語句: 應用範圍:選擇結構和循環結構 優化
continue語句:應用於循環結構 spa
注: 設計
a,這兩個語句離開應用範圍,存在是沒有意義的. code
b,這個兩個語句單獨存在下面都不能夠有語句,由於執行不到. 內存
c,continue語句是結束本次循環繼續下次循環.
d,標號的出現,可讓這兩個語句做用指定的範圍
class OtherDemo { public static void main(String[] arg) { //break w:for(int x= 0; x<3;x++) { q:for(int y=0; y>4; y++) { System.out.println("x="+x); break w; } } //continue:只能做用於循環結構.特色:結束本次循環,繼續下一次循環 for(int x=0;x<3;x++) { if(x%2==1) continue; System.out.println("x="+x); } w:for(int x= 0; x<3;x++) { q:for(int y=0; y>4; y++) { System.out.println("x="+x); continue w; } } /* 記住: 1,break和continue語句做用的範圍. 2,break和continue單獨存在時,下面能夠有任何語句,由於都執行不到 */ } }
/* ----* ---* * --* * * -* * * * * * * * * */ class ForForTest2 { public static void main(String[] arg) { for(int x=0;x<5;x++) { for(int y=x+1;y<5;y++) { System.out.print("-"); } for(int z=0 ;z<=x ;z++) { System.out.print("* "); } System.out.println(); } } }
函數的定義
函數的特色
函數的應用
函數的重載
什麼是函數?
函數就是定義在類中的具備特定功能的一段獨立小程序.
函數也稱爲方法
函數的格式:
修飾符 返回值類型 函數名(參數類型 形式參數1,參數類型 形式參數2,...)
{
執行語句;
return返回值;
}
返回值類型:函數運行後的結果的數據類型.
參數類型:是形式參數的數據類型.
形式參數:是一個變量,用戶存儲調用函數時傳遞給函數的實際參數.
實際參數:傳遞給形式參數的具體數值.
return:用戶結束函數.
返回值:該值會返回給調用者.
定義函數能夠將功能代碼進行封裝
便於對該功能進行復用
函數只有被調用纔會執行
函數的出現提升了代碼的複用性
對於函數沒有具體返回值的狀況,返回值類型用關鍵字void表示,那麼該函數中的return語句若是在最後一行能夠省略不寫.
注意:
函數中只能調用函數,不能夠在函數內部定義函數.
定義函數時,函數的結果應該返回給調用者,交由調用者處理
class FunctionDemo { public static void main(String[] arg) { /* int x= 4; System.out.println(x*3+5); x = 6; System.out.println(x*3+5); */ //int y = 4*3+5; //int z = 6*3+5; getResult(4); //在這裏函數中不能定義函數,只能調用函數 } //發現以上的運算,由於獲取不一樣數據的運算結果,代碼出現了重複. //爲了提升代碼的複用性,對代碼進行抽取. //將這個部分定義成一個獨立的功能.方便與往後使用. //Java中對功能的定義是經過函數的形式來體現的. //須要定義功能,完成一個整數的*3+5的運算,並打印結果. //1.先明確函數定義的格式. // /* 修飾符 返回值類型 函數名(參數類型 形式參數1,參數類型 形式參數2,...) { 執行語句; return返回值; } //當函數運算後,沒有具體的返回值時,這是返回值類型用一個特殊的關鍵字來//標識該關鍵字就是void, void表明的是函數沒有具體返回值的狀況 //當函數的返回類型是void時,函數中的return語句能夠省略不寫 */ public static void getResult(int num) { System.out.println(num * 3 + 5); return;//能夠省略 } }
兩個明確
明確要定義的功能最後的結果是什麼?
明確在定義功能的過程當中,是否須要未知內容參與預算
class FunctionTest { public static void main(String[] arg) { draw(5,6); printHr(); draw(7,9); printHr(); print99(); } /* 2,定義一個打印99懲罰表功能的函數 */ public static void print99() { for(int x=1; x<=9; x++) { for(int y=1; y<=x; y++) { System.out.print(y+"*"+x+"="+y*x+"\t"); } System.out.println(); } } /* 1.定義一個功能,用戶打印矩形 思路: 1,肯定結果:沒有,由於直接打印,因此返回類型是void 2,有未知內容嗎?有,兩個,由於矩形的航和列不肯定. */ public static void draw(int row,int col) { for(int x = 0; x<row;x++) { for(int y = 0; y<col; y++) { System.out.print("*"); } System.out.println(); } } public static void printHr() { System.out.println("----"); } }
重載的概念
在同一個類中,容許存在一個以上的同名函數,只要他們的參數個數或者參數類型不一樣便可.
重載的特色
與返回值類型無關,只看參數列表
重載的好處:
方便於閱讀,優化了程序設計
重載示例:
//返回兩個整數的和
int add(int x, int y){return x+y;}
//返回三個整數的和
int add(int x, int y, int z){return x+y+z;}
//返回兩個小數的和
double add(double x,double y){return x+y;}
/*何時用重載
當定義的功能相同,但從那與的運算的未知內容不一樣.
那麼,這時就定義一個函數名稱以表示起功能,方便閱讀,而經過參數列表的不一樣來區分多個同名函數.
*/
class FunctionOverload { public static void main(String[] arg) { //add(4,5); //add(4,5,6); //System.out.println(); print99(); } public static void print99(int num) { for(int x =1; x<=num; x++) { for(int y=1;y<=x; y++) { System.out.print(y+"*"+x+"="+y*x+"\t"); } System.out.println(); } } //打印99乘法表 public static void print99() { print99(9); } //定義一個加法運算,或許兩個整數的和. public static int add(int x, int y) { return x+y; } //定義一個加法,獲取三個整數的和. public static int add1(int x, int y, int z) { return add(x,y)+z; } }
/* void show(int a,char b,double c){} a. void show(int x,char y,double z){}//沒有重載,由於和原函數同樣. b. int show(int a, double c,char b){}//重載,由於參數類型不一樣.注意,重載和返回值類型不一樣. c. void show (int a,double c,char b){}//重載,由於參數類型不一樣.注意,重載和返回值類型不一樣. d. boolean show (int c,char b){}重載了,由於參數個數不一樣. e. void show (double c){}重載了,由於參數個數不一樣. f. double show (int x,char y,double z){}沒有,這個函數不能夠和給定的函數同時在一個類中 */
概念
同一種類型數據的集合.其實數組就是一個容器.
添加好友
小額贊助
微信 |
支付寶 |
|
|
|
|
|
|
|
|
|
|