Java基礎三種基本結構
一、順序結構
順序結構就是我們最常見的按照順序從上到下依次執行
package com.struct;
public class ShunXu_Struct {
public static void main(String[] args) {
//struct 結構:學習第一個順序結構
System.out.println("順序結構1");
System.out.println("順序結構2");
System.out.println("順序結構3");
System.out.println("順序結構4");
}
}
二、選擇結構
If分支
格式:if(條件){業務邏輯}
package com.struct;
import java.util.Scanner;
public class ifDemo03 {
public static void main(String[] args) {
// if多選擇結構
Scanner scan = new Scanner(System.in);
System.out.println("請輸入你的分數:");
int score = scan.nextInt();
if (score==100){
System.out.println("恭喜滿分");
}else if (score<100 && score>=90){
System.out.println("優秀");
}else if (score<90 && score>=80){
System.out.println("良好");
}else if (score<80 && score>=70){
System.out.println("中等");
}else if (score<70 && score>=60){
System.out.println("及格");
}else if (score<60 ){
System.out.println("不及格");
}else {
System.out.println("成績不合法");
}
scan.close();
}
}
Switch_Case分支
可以通過反編譯的信息去查看字節碼文件,可以發現Case那個地方是一串式子因為字符的本子就是數字,每個Case后面都要加上break表示當條件滿足遍跳出循環,否則會出現case穿透現象。
格式:switch(條件){case1: case2}不同條件的不同結果
package com.struct;
public class switchCase_2 {
public static void main(String[] args) {
//Jdk7以上支持字符串
//字符的本質還是數字
//反編譯 java---class(字節碼文件) ---反編譯(idea)
String name = "大哥";
switch (name){
case "大哥":
System.out.println("我是你大哥");
break;//可選
case "二弟":
System.out.println("我是你二弟");
break;
case "三弟":
System.out.println("我是你三弟");
break;
default://可寫可不寫
System.out.println("你這是哪國語言???");
}
}
}
三、循環結構
While循環
package com.struct;
public class do_WhileDemo01 {
public static void main(String[] args) {
/**
* while(布爾表達式) {循環內容} 先判斷在執行如果條件不滿足就不會執行
* while是最基本的循環,只要布爾表達式為true,循環就會一直執行下去
* 大多數情況我們是會讓循環停止下來的,這就需要一個讓表達式失效的方式結束循環
* 少部分情況需要循環體一直執行,比如服務器的請求響應監聽
* 循環條件如果一直為ture就會造成死循環,在正常的業務編程中會影響性能或者程序卡死
*
* 思考如何實現 計算1+2+3+4+5+.....+100=?
*/
int i = 0;
int sum =0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
Do While循環
package com.struct;
public class do_WhileDemo02 {
public static void main(String[] args) {
/**
* 首先要明白 While 和 do While的區別
* While循環是先判斷再執行也就是說如果條件不滿足他壓根就不會執行
* 但是有時候我們需要即使不滿足條件也要執行一次程序
* 最大的區別就是 do While 至少會執行一次
*/
int i = 0;
int sum =0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
While 和 do While的區別
package com.struct;
public class dowhile_And_while {
public static void main(String[] args) {
/**
* 測試一下 While 和 do while的不同
* 可以看出來do{}while(條件)至少執行了一次程序
*/
int a = 0;
// 測試While
while (a<0) {
System.out.println("a");//這一段沒有執行
}
System.out.println("================");
do {
a++;
System.out.println(a); //結果 1 可以看出來就算條件不滿足也最少執行了一次
}while (a<0);
}
}
For循環
package com.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1 ; //初始化條件
int sum = 0 ;
while (a<=100){//條件判斷
System.out.println(a);//循環體
a += 2 ;//迭代
}
System.out.println("While循環結束");
// 初始化值 循環條件 迭代更新
for (int i = 1;i<=100;i++){//循環條件
System.out.println(i);//循環體
sum = sum + i;
}
System.out.println("for循環結束!sum的和為"+sum);
//for 循環語句是支持迭代的一種通用結構,是最有效,最靈活的循環結構
//快捷方式生成for循環100.for
}
}
For循環小練習
計算0-100之間奇偶數的和
package com.struct;
public class ForDemo02 {
public static void main(String[] args) {
//練習1:計算0到100之間奇數和偶數的和
int oddSum = 0;//奇數的和
int evenSum = 0;//偶數的和
for (int i = 0; i <= 100; i++) {
if (i%2!=0){//奇數
oddSum +=i;
}else {//偶數
evenSum +=i;
}
}
System.out.println("奇數的和是:"+oddSum+"\n"+"偶數的和是:"+evenSum);
}
}
使用While或for循環輸出1-1000之間能被5整數的數,且每行輸出3個
package com.struct;
public class ForDemo03 {
public static void main(String[] args) {
//練習2:使用while或for 循環輸出1-1000之間能被5整除的數,且每行輸出3個
//第一種for循環的實現
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
//被5整除的數
System.out.print(i+"\t");
}
if (i%(5*3)==0){//5、10、15 以15為整除來換行
//System.out.print("\n");//換行
System.out.println();//換行
//print()不換行 println()換行
}
}
System.out.println("for循環結束開始while循環練習.....");
// 第二種While循環的方式實現
int a = 0;
while (a<=1000){
a++;
if (a%5==0){
System.out.print(a+"\t");
}
if (a%(15)==0){
System.out.println();
}
}
System.out.println("while循環結束");
}
}
for循環打印九九乘法表
package com.struct;
public class ForDemo04 {
public static void main(String[] args) {
// 練習三 for循環實現 九九乘法表 嵌套for循環的使用
//1.首先輸出一個定式子的1+"*"+i=(1*i) 能夠得出一列 以1開頭*到9的數值 ===>先寫出內循環
//2.把九九乘法表看成1*1 開頭的列 2*2開頭的列 3*3開頭的列
//3.去掉重復值 也可以理解為控制每一行的長度嘛 外循環執行一次內訓都要重新執行一遍 所以當i<=j的時候就可以達到效果了
for (int j = 1; j < 10; j++) {
System.out.println();//執行完一次就換行
for (int i = 1; i<=j; i++) {
System.out.print(i+"*"+j+"="+(j*i)+"\t");
}
}
}
}
打印金字塔
要學會吧困難的問題簡單化把他看成是多個三角形的拼接 分成三部分來實現
package com.struct;
public class TestDemo {
public static void main(String[] args) {
// 小結練習 金字塔三角形打印
// 如果理解不了可以進行debug斷點測試
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >=i; j--) {//左上角三角形
System.out.print(" ");
}
for (int j =1; j <=i; j++) {//左下角三角形
System.out.print("*");
}
for (int j =1; j <i; j++) {//又下腳小三角形
System.out.print("*");
}
System.out.println();
}
}
}
結果:
*
***
*****
*******
*********
增強for循環
增強for循環其實就是一種方便數組遍歷數據的一個循環
package com.struct;
public class ForDemo05_Add {
public static void main(String[] args) {
//增強for循環
int[] numbers = {10,20,30,40,50};//定義一個數組
for (int i =0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("普通for循環演示結束開始增強for循環的演示.....");
//增強for循環遍歷數組元素
for (int x:numbers){
System.out.println(x);
// numbers里面每一項的值賦值給了int x
}
/*
結果如下:
10
20
30
40
50
普通for循環演示結束開始增強for循環的演示.....
10
20
30
40
50
*/
}
}
四、跳出循環的方式
首先要明白跳出循環的方式宏觀來看有三種
- 編輯業務代碼讓循環條件有ture變為false從而跳出循環
- break;在循環的主體,可以直接跳出循環
- continue;跳出本次循環,但是會對下一次循環繼續進行判斷,為ture時還是會繼續執行
break的使用
package com.struct;
public class BreakDemo {
public static void main(String[] args) {
// break用於強行退出循環,不執行循環中剩余的語句
int a = 0;
int sum = 0;
while (a<100){
a++;
System.out.println(a);
if (a==11){
System.out.println("a=11了兄弟循環要結束了喔!");
break;
}
}
System.out.println("123456你看見我了那我結束循環了");
}
}
continue的使用
package com.struct;
public class ContinueDemo {
public static void main(String[] args) {
/*
continue 語句用在循環語句體中,用於終止某次循環過程,也就是跳過循環體中沒有執行的語句
*/
int a =0;
while (a<100){
a++;
if (a%10==0){//可以看出每次滿足a%10==0的時候就會跳出本次循環不輸出相應結果 但他還是會繼續回到While執行下一次的循環
System.out.println();
continue;//if條件滿足直接跳回開頭while執行下一個while循環條件的判斷
}
System.out.print(a+"\t");
/*結果
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
*/
}
}
}