循環結構
while do-while for
while語句格式
while(條件表達式){
循環體;
}
do-while語句格式
do{
循環體;
}while(條件表達式);
for語句格式
for(初始化表達式;循環條件表達式;循環後的操作表達式){
循環體;
}
例子如下:
/*
循環結構
*/
class Demo2
{
public static void main(String[] args)
{
/*
打印兩次你妹
int i=1;
while (i<3)
{
System.out.println("你妹");
i++;//不要會死循環的
}
*/
/*
打印兩次你妹
int i=1;
do{
System.out.println("你妹");
i++;//不要會死循環的
}while(i<3);
*/
/*
打印三次 二龍xp
for (int j=0;j<3 ;j++ )
{
System.out.println("二龍xp");
}
*/
/*
最簡單的無限循環 while for
while(true){};
for(;;){};
*/
/*
此代碼編譯出錯 條件表達式只能爲真 假
for (System.out.println("a"); System.out.println("b"); System.out.println("c"))
{
System.out.println("d") ;
}
*/
/*
此代碼編譯通過 a dcdcdc
int i=0;
for (System.out.println("a"); i<3; System.out.println("c"))
{
System.out.print("d") ;
i++;
}
*/
/*
效果同上
*/
int i=0;
for (System.out.println("a"); i<3;i++,System.out.println("c"))
{
System.out.print("d");
}
}
}
while和do-while之間的區別:
1.while:先判斷後執行,do while是先執行在判斷.
所以do while至少執行一次,而while不會
for和while不同:
1.for裏面的變量有作用域範圍,只在for循環裏面有效
2.for與while可以互換,但是需要循環增量首選for
for節省內存空間,利於內存優化.
/*
循環測試類
*/
class Demo3
{
public static void main(String[] args)
{
/*
需求:1-10的累加
累加思想:通過變量紀錄每次變化的結果,通過循環進行累加動作
int sum=0;
for (int i=1;i<=10 ;i++ )
{
sum+=i;
}
System.out.println(sum);
*/
/*
需求: 1-100之間,7的倍數由多少個?
計數器思想:
通過一個變量記住數據的狀態變化,通過循環完成.
*/
int count=0;
for (int i=1;i<=100 ;i++ )
{
if(i%7==0)
count++;
}
System.out.println(count);
}
}
什麼時候使用循環結構?
當要對某些語句執行多次時,就使用循環結構.
條件表達式只能爲真或假,for不寫表達式默認爲true
循環注意: 一定要明確那些語句需要循環,那些不要.
for嵌套
/*
循環測試類
*/
class Demo4
{
public static void main(String[] args)
{
/*
需求:
*****
****
***
**
*
for (int i=0;i<5 ;i++ )
{
for (int j=0;j<5-i ;j++ )
{
System.out.print("*");
}
System.out.println();
}
*/
/*
需求:
*
**
***
****
*****
for (int i=0;i<5 ;i++ )
{
for (int j=0;j<=i ;j++ )
{
System.out.print("*");
}
System.out.println();
}
*/
/*
需求:
1
12
123
1234
12345
for (int i=1;i<=5 ;i++ )
{
for (int j=1;j<=i ;j++ )
{
System.out.print(""+j);
}
System.out.println();
}
*/
/*
需求:九九乘法表
for (int i=1;i<=9 ;i++ )
{
for (int j=1;j<=i ;j++ )
{
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
*/
/*
需求:標號的使用
o:for (int i=0;i<3 ;i++ )
{
System.out.println("外層循環");
t:for (int j=0;j<3 ;j++ )
{
if(j%2!=0)
continue t;
System.out.println("內層循環");
}
}
*/
/*
需求:
*
* *
* * *
* * * *
* * * * *
*/
for (int i=0;i<5 ;i++ )
{
for (int j=0;j<5-i-1 ;j++ )
{
System.out.print(" ");
}
for (int k=0;k<=i ;k++ )
{
System.out.print("* ");
}
System.out.println();
}
}
}
其他流程控制語句: continue break;
break:應用範圍:選擇結構和循環結構 作用:中斷循環
continue:應用範圍: 循環結構 作用:結束本次循環,進入下一次循環
標號: 例如 w: 用於循環結構上,給循環起名字
break,continue後面的不能有語句,因爲不會執行到.
函數:
什麼是函數?
函數(方法)就是定義在類中具有特定功能一段獨立小程序
好處:提高代碼複用性
定義語法:
修飾符 返回值類型 函數名(參數列表){
方法體;
}
當函數運算後,沒有返回值,可以用到java一個特殊關鍵字void,可以省略return語句;
但有返回值時,一定要返回值
java最小的功能就是函數
注意:不可以在方法內部定義函數
如何定義一個函數呢?
1.一個函數名體現函數的功能
2.明確函數是否要有參與運算的參數
3.明確函數是否要返回值
返回值爲void不能被輸出語句輸出
函數的重載:
在同一類中,允許有一個以上同名的函數,參數類型和參數個數不同即可.
什麼時候用重載?
在定義功能相同時,但參與運算未知內容不同,那麼這時就定義一個函數名稱以表示
其功能,方便閱讀,而通過參數列表不同來區分多個函數.
/*
函數測試類
*/
class Demo5
{
public static void main(String[] args)
{
//System.out.println(getSum(5,5));
//print99();
System.out.println(getSum());
}
/*
定義一個功能,返回3+4運算的結果
*/
public static int getSum(){
return getSum(3,4);
}
/*由用戶輸入兩個數,進行求和運算*/
public static int getSum(int n,int n1){
return n+n1;
}
/*判斷兩個數是否相等*/
public static boolean isEquals(int n,int n1){
return n==n1;
}
/*定義一個功能打印九九乘法表*/
public static void print99(){
for (int i=1;i<=9 ;i++ )
{
for (int j=1;j<=i ;j++ )
{
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
數組:同一個數組數據的集合,其實就是一個容器
好處:給元素有自動編號
定義格式:
數據類型[] 數組名=new 數據類型[長度];
使用數組
數組名[下標];
/*
數組測試
*/
class Demo6
{
public static void main(String[] args)
{
//數組定義: 數據類型 []數組名 = new 數據類型[長度];
int [] arr=new int[2];//定義了一個數組長度爲2的int型數組
//堆內存實體內的數據都具有默認初始值 int的初始值是0
arr[0]=15;//給下標爲0的元素賦值
System.out.println(arr[0]); // 最小下標0
System.out.println(arr[arr.length-1]); // 最大下標 arr.length-1
}
}
1.方法區
2.本地方法區
3.寄存器
4.棧內存 局部變量,資源會自動釋放
5.堆內存 存實體對象,實體對象內基本數據具有默認值
堆內存中當一個實體之間沒有引用的時候,jvm會視爲垃圾,會啓動java
垃圾回收機制不定時處理這些垃圾.
只要是new都會在內存開闢一片新的內存空間
—————————— ASP.Net+Android+IOS開發、.Net培訓、期待與您交流!——————————