java 循环关键字_Java循环结构_常量_关键字
一、循環
定義:當滿足一定條件的時候,重復執行某一段代碼的操作
while、for、do...while是Java的循環
二、While循環
語法格式:
int i = 0;//初始化值
while(i<10){ //循環判斷
?循環體 循環重復執行的代碼
?循環控制語句
}
解析While循環
初始化條件【定義初始變量、只執行一次】
循環判斷條件語句【執行的次數比循環體多一次、決定著循環能不能執行下一個語句】
循環體【被重復執行的代碼、循環真正體現的功能】
循環控制語句【控制循環條件變化、缺失可能造成死循環】
案例:
class Demo04 {
public static void main(String[] args) {
/*
輸出所有的水仙花數字
1、確定初始化值
100
2、開啟循環,確定結束條件
999
3、計算得出水仙花數并輸出
獲取數字的各位字面值
計算字面值的立方和
比較立方和是否等于數字本身
*/
int i = 100;
while(i <= 999){
// 獲取個位十位百位數字123
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
// 計算立方和
int sum = ge*ge*ge + shi*shi*shi +bai*bai*bai;
// 比較、判斷
if(sum == i){
System.out.println("水仙花數:" + i);
}
// 控制循環判斷變化
i++;
}
}
}
三、while循環嵌套
定義:while循環中執行的循環內容還是while循環
輸出九九乘法表(案例):
public class Demo06 {
public static void main(String[] args) {
/**
* while循環輸出乘法表
*/
int i = 1;
while(i <= 9) {
// 內層循環,控制輸出的列數
int j = 1;
while(j <= i) {
System.out.print(j + "*" + i + "=" + (i*j) + "\t");
j++;
}
System.out.println();
i++;
}
}
}
四、do....while
語法格式:
語法格式:
int i = 0;//初始化值
do{
循環體 循環重復執行的代碼
}
while(i>10);
解析do...while();循環
循環開始后先執行do后面的代碼,第一次執行不用經過循環條件判斷
第一次執行結束后在判斷下一次能否執行
案例:
class Demo08 {
public static void main(String[] args) {
int i = 0;
// 先執行do后面的代碼----這部分代碼和while循環后面的代碼書寫方式、意義是相同的
do{
// 循環重復執行的代碼
System.out.println("Hello World!");
// 條件控制語句
i++;
// 條件判斷
}while(i > 0);
System.out.println("OVER!");
}
}
五、for循環
語法格式:
for(初始化語句; 循環條件判斷語句; 訓話條件控制語句){循環體}
解析for循環循環
for(int i = 0;i < 5;i++){
System.out.println("Hello World!" + i);
}
System.out.println("OVER!");
執行過程
第一次
初始化變量int i = 0,判斷i < 5,判斷結果true,執行for循環中的循環體
第二次
執行循環條件控制代碼i++,i變成1,判斷i < 5,判斷結果true,執行for循環中的循環體
第三次
執行循環條件控制代碼i++,i變成2,判斷i < 5,判斷結果true,執行for循環中的循環體
第四次
執行循環條件控制代碼i++,i變成3,判斷i < 5,判斷結果true,執行for循環中的循環體
第五次
執行循環條件控制代碼i++,i變成4,判斷i < 5,判斷結果true,執行for循環中的循環體
第六次
執行循環條件控制代碼i++,i變成5,判斷i < 5,判斷結果false,不執行for循環中的循環體,循環結束
案例:
import java.util.Scanner;
class Demo14 {
public static void main(String[] args) {
// 鍵盤錄入一個數字,計算它的階乘
/*
1、導入Scanner
2、創建Scanner對象
3、提示輸入一個數字
4、獲取輸入的數字
5、編寫循環,從1開始,到num結束
6、在循環中不斷計算乘積
7、循環結束后輸出結果
*/
Scanner in = new Scanner(System.in);
System.out.println("你想計算到幾的階乘?");
int num = in.nextInt();
int mul = 1;
// 開啟循環
for(int i = 1;i <= num;i++){
mul *= i;
}
System.out.println("num階乘的結果是:" + mul);
}
五、for循環嵌套
定義:for循環中再書寫一個for循環
直角三角形(案例):
public class Demo03 {
public static void main(String[] args) {
// 確定打印的行數
for (int i = 0; i < 9; i++) {
// 確定打印的列數
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println("==================");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < (9-i); j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
六、流程控制(braeak、continue)
break:終止本次循環的執行
案例:
class Demo16 {
public static void main(String[] args) {
// break的使用
int i = 0;
while(i <= 100){
System.out.println("Hello World!" + i);
// 在執行到i==66的時候停止運行
if(i == 66){
// 使用break終止循環的執行
break;
}
i++;
}
System.out.println("Hello World!");
}
}
continue:跳過本次循環后面的內容,進入到下一輪循環的執行
案例:
class Demo17 {
public static void main(String[] args) {
// continue
int i = 1;
while(i <= 20){
// 輸出所有的奇數
if(i % 2 != 0){
System.out.println(i);
}
i++;
}
System.out.println("Hello World!001");
i = 1;
while(i <= 20){
System.out.println(i);
i += 2;
}
System.out.println("Hello World!002");
i = 0;
while(i <= 20){
// 判斷:如果是偶數,下面的內容就跳過
i++;
if(i % 2 == 0){
continue;
}
System.out.println(i);
}
System.out.println("Hello World!003");
}
}
七、常量(final)
字面值常量:例如:1、2、3
自定義常量:定義一些不需要也不能被改變的數值。如主機地址、數據庫鏈接時候的用戶名,密碼、端口號等
定義方式:所有的字母大寫,多個字母使用下劃線連接
八、關鍵字
定義:java關鍵字是被java語言賦予了特殊含義的標識符
例如:class、void、public、static、while、if等。
總結
以上是生活随笔為你收集整理的java 循环关键字_Java循环结构_常量_关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 读取 远程文件_利用JAVA获
- 下一篇: java http客户端_java 11