Java基礎(程序控制結構篇)

Java的程序控制結構與C語言一致,分為順序結構、選擇結構(分支結構)和循環結構三種。

一、順序結構

如果程序不包含選擇結構或是循環結構,那么程序中的語句就是順序的逐條執行,這就是順序結構。

import java.util.Scanner;
public class SequenceConstruct{public static void main(String[] args){//以下就使程序的順序結構//語句是從上到下逐個執行的,沒有跳轉int a = 10;char b = 'a';double c = 1.23;String str = "";Scanner scanner = new Scanner(System.in);System.out.print("請輸入一句話:");str = scanner.next();System.out.println(str);}}

二、選擇結構

1. if-else

在if-else分支結構中,else會與上方最近的if匹配。

1.1 單分支

在這里插入圖片描述

//單分支
import java.util.Scanner;
public class SelectConstruct01{public static void main(String[] args){String str = "";System.out.println("請輸入一個名字:");Scanner scanner = new Scanner(System.in);str = scanner.next();if("jack".equals(str))System.out.println("你輸入的名字是jack");}}

在這里插入圖片描述

1.2 雙分支

import java.util.Scanner;public class SelectConstruct02{public static void main(String[] args){//雙分支//System.out.print("請輸入你的名字:");Scanner scanner = new Scanner(System.in);String name = scanner.next();if("jack".equals(name))System.out.println("你的名字是jack");elseSystem.out.println("你的名字不是jack");}}

在這里插入圖片描述

1.3 多分支

在這里插入圖片描述

import java.util.Scanner;public class SelectConstruct03{public static void main(String[] args){//多分支//輸入保國同志的芝麻信用分:// 如果:// 1) 信用分為 100 分時,輸出 信用極好;// 2) 信用分為(80,99]時,輸出 信用優秀;// 韓順平循序漸進學 Java 零基礎// 第 100頁// 3) 信用分為[60,80]時,輸出 信用一般;// 4) 其它情況 ,輸出 信用 不及格// 5) 請從鍵盤輸入保國的芝麻信用分,并加以判斷Scanner scanner = new Scanner(System.in);System.out.println("請輸入信用分:");int score = scanner.nextInt();if(score > 100 || score < 0)System.out.println("信用分輸入有誤!");else if(score == 100)System.out.println("信用極好");else if(score > 80)System.out.println("信用優秀");else if(score >= 60)System.out.println("信用一般");elseSystem.out.println("信用不及格");}}

在這里插入圖片描述

1.4 嵌套分支

在這里插入圖片描述

import java.util.Scanner;public class SelectConstruct04{public static void main(String[] args){//嵌套分支//在一個分支結構中嵌套了另一個分支結構//參加歌手比賽,如果初賽成績大于 8.0 進入決賽,否則提示淘汰。//并且根據性別提示進入男子組或女子組。Scanner scanner = new Scanner(System.in);System.out.print("請輸入初賽成績:");double score = scanner.nextDouble();System.out.print("請輸入性別:");char sex = scanner.next().charAt(0);if(score > 8.0)if(sex == '男')System.out.println("進入男子組");else if(sex == '女')System.out.println("進入女子組");elseSystem.out.println("性別輸入有誤");elseSystem.out.println("淘汰");}}

在這里插入圖片描述

2. switch

  • switch括號中的表達式結果類型必須是(byte,short,int,char,enum,String)中的一種。
  • case后的常量類型必須與switch括號中表達式結果的類型一致,或是可以自動轉換(switch括號中的類型轉換成case關鍵字后的類型)成可以比較的類型。
  • case后必須是常量,不能是變量。
  • default是可選的。
  • break用于跳出當前switch語句塊,如果沒有break關鍵字,那么就會發生穿透,語句會一直執行到switch語句塊的末尾或是遇到break。
    在這里插入圖片描述
import java.util.Scanner;
public class SwitchStructrue{public static void main(String[] args){Scanner scanner = new Scanner(System.in);boolean flag = true;while(flag){System.out.println("輸入1表示退出循環:");if(scanner.nextInt() == 1){flag = false;continue;}System.out.print("輸入一個字符(a-g):");char input = scanner.next().charAt(0);switch(input){case 'a':System.out.println("Monday");break;case 'b':System.out.println("Tuesday");break;case 'c':System.out.println("Wensday");break;case 'd':System.out.println("Thursday");break;case 'e':System.out.println("Friday");break;case 'f':System.out.println("Saturday");break;case 'g':System.out.println("Sunday");break;default:System.out.println("error,please input again");}	}	}
}

在這里插入圖片描述

3. switch與if-else的比較

  • 如果判斷的數值不多,并且是固定不變的,例如星期、月份等內容,推薦使用switch。
  • 對區間的判斷,結果為boolean類型的判斷等,使用if-else。

三、循環結構

1. for循環

for循環的結構:for(循環變量初始化;循環條件;循環變量迭代){循環體}.可以一次性初始化多個變量(用逗號隔開),但是它們的類型要一致,循環變量的迭代處也可以有多條語句(用逗號隔開)。
在這里插入圖片描述

public class ForStructrue{public static void main(String[] args){for(int i = 1; i <= 9; i++){for(int j = 1; j <= i; j++){String str = j + "*" + i + " = " +  i * j;System.out.print(str + "  ");}System.out.println();}}
}

在這里插入圖片描述

2. while循環

while循環的結構:while(循環條件){循環體}.
在這里插入圖片描述

public class WhileStructrue{public static void main(String[] rags){int i = 1, j = 1;while(i <= 9){j = 1;while(j <= i){System.out.print(j+"*"+i+"="+i*j+"  ");j++;}System.out.println();i++;}}
}

在這里插入圖片描述

3. dowhile循環

dowhile循環與while循環基本一樣,除了當初始條件不滿足時,dowhile會執行一次,而while一次都不會執行。注意while括號后有分號。
在這里插入圖片描述

public class DoWhileStructrue{public static void main(String[] args){boolean flag = false;while(flag){System.out.println("This is while");}do{System.out.println("This is dowhile");}while(flag);}
}

在這里插入圖片描述

4. 多重循環

多重循環就是一層循環為另一個循環的循環體,打印乘法表就需要使用多重循環來完成,下面使用多重循環打印金字塔。

import java.util.Scanner;
public class MulCirculation{public static void main(String[] args){System.out.println("輸入要打印的金字塔規模:");Scanner scanner = new Scanner(System.in);int num = scanner.nextInt();for(int i = 1; i <= num; i++){int j = 0;while(j < num - i){System.out.print(" ");j++;}for(j = 0; j < 2 * i - 1; j++){System.out.print("*");}System.out.println();}}
}

在這里插入圖片描述

5. break關鍵字

用于跳出當前層循環語句或跳出switch語句塊。可以使用標簽來指定跳出哪一層循環(盡量不要使用標簽)。

public class BreakTest{public static void main(String[] args){for(int i = 1; i <= 100; i++){if(i == 49) break;System.out.print(i + " ");}System.out.println();for(int i = 1; i <= 5; i++){for(int j = 1; j <= 5; j++){if(j == i) break;System.out.print(i*j+" ");}System.out.println();}circulation1:for(int i = 1; i <= 10; i++){circulation2:for(int j = 1; j <= 3; j++){circulation3:for(int k = 1; k <= 3; k++){if(i == 1){break circulation2;}System.out.println("i = " + i + " j = " + j + " k = " + k);if(i == 3) break circulation1;}}}}
}

在這里插入圖片描述

6. continue關鍵字

用于跳過本次迭代時continue關鍵字之后的所有語句,并進行下一次迭代,但不會跳過for循環中循環變量的迭代語句。可以使用標簽指定層次。

public class ContinueTest{public static void main(String[] args){for(int i = 1; i <= 3; i++){for(int j = 1; j <= 3; j++){if(i == j) continue;System.out.print("i = " + i + " j = " + j + "  ");}System.out.println();}circulation1:for(int i = 1; i <= 3; i++){circulation2:for(int j = 1; j <= 3; j++){circulation3:for(int k = 1; k <= 3; k++){if(i == 2) continue circulation1;if(j == 1) continue circulation2;System.out.print("i = " + i + " j = " + j + " k = " + k + "  ");}System.out.println();}}}
}

在這里插入圖片描述

7. return關鍵字

return關鍵字用于跳出所在方法。

public class ReturnTest{public static void main(String[] args){int i = 1;while(i <= 10){if(i == 6) return;System.out.println("i = " + i++);}System.out.println("在main方法中");}
}

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/160357.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/160357.shtml
英文地址,請注明出處:http://en.pswp.cn/news/160357.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【 OpenGauss源碼學習 —— 列存儲(CStoreMemAlloc)】

列存儲&#xff08;CStoreMemAlloc&#xff09; 概述CStoreMemAlloc 類CStoreMemAlloc::Palloc 函數CStoreMemAlloc::AllocPointerNode 函數CStoreMemAlloc::FreePointerNode 函數CStoreMemAlloc::Repalloc 函數CStoreMemAlloc::PfreeCStoreMemAlloc::Register 函數CStoreMemAl…

杭電oj 2064 漢諾塔III C語言

#include <stdio.h>void main() {int n, i;long long sum[35] { 2,8,26 };for (i 3; i < 35; i)sum[i] 3 * sum[i - 1] 2;while (~scanf_s("%d", &n))printf("%lld\n", sum[n - 1]); }

問鼎web服務

華子目錄 www簡介常見Web服務程序介紹&#xff1a;服務器主機主要數據瀏覽器網址及http介紹urlhttp請求方法 http協議請求的工作流程www服務器類型靜態網站動態網站 快速安裝Apache安裝準備工作httpd所需目錄主配置文件 實驗操作 www簡介 Web網絡服務也叫www&#xff08;world…

編碼的發展歷史

編碼的發展歷史 ASCII&#xff1a; ASCII編碼使用7位二進制數表示一個字符&#xff0c;范圍從0到127。每個字符都有一個唯一的ASCII碼值與之對應。例如&#xff0c;大寫字母"A"的ASCII碼是65&#xff0c;小寫字母"a"的ASCII碼是97。 ASCII字符集包括英文…

linux服務器安裝gitlab

一、安裝gitlab sudo yum install curl policycoreutils-python openssh-server openssh-clients sudo systemctl enable sshd sudo systemctl start sshd sudo firewall-cmd --permanent --add-servicehttp curl https://packages.gitlab.com/install/repositories/gitla…

LabVIEW中將SMU信號連接到PXI背板觸發線

LabVIEW中將SMU信號連接到PXI背板觸發線 本文介紹如何將信號從PXI&#xff08;e&#xff09;SMU卡路由到PXI&#xff08;e&#xff09;機箱上的背板觸發線。該過程涉及使用NI-DCPowerVI將SMU信號導出到PXI_TRIG線上。 在繼續操作之前&#xff0c;請確保在開發PC上安裝了兼容版…

MySQL啟動MySQL8.0并指定配置文件

MySQL啟動MySQL8.0并指定配置文件 mkdir -p /mysql8hello/config ; mkdir -p /mysql8hello/data ; mkdir -p /mysql8hello/logs; mkdir -p /mysql8hello/conf; vim /mysql8hello/config/my.cnf; # 啟動報錯就修改成777&#xff0c;但是會提示風險 chmod 644 /mysql8hello/co…

d3dx9_43.dll缺失怎么辦?教你一分鐘修復d3dx9_43.dll丟失問題

今天&#xff0c;與大家分享關于“d3dx9_43.dll丟失的5個解決方法”的主題。在我們的日常生活和工作中&#xff0c;我們可能會遇到各種各樣的問題&#xff0c;而d3dx9_43.dll丟失就是其中之一。那么&#xff0c;什么是d3dx9_43.dll呢&#xff1f;它為什么會丟失&#xff1f;又該…

【LeetCode刷題-鏈表】--25.K個一組翻轉鏈表

25.K個一組翻轉鏈表 思路&#xff1a; 把鏈表節點按照k個一組分組&#xff0c;可以使用一個指針head依次指向每組的頭節點&#xff0c;這個指針每次向前移動k步&#xff0c;直至鏈表結尾&#xff0c;對于每個分組&#xff0c; 先判斷它的長度是否大于等于k&#xff0c;若是&am…

什么是Zero-shot(零次學習)

1 Zero-shot介紹 Zero-shot學習&#xff08;ZSL&#xff09;是機器學習領域的一種先進方法&#xff0c;它旨在使模型能夠識別、分類或理解在訓練過程中未見過的類別或概念。這種學習方法對于解決現實世界中常見的長尾分布問題至關重要&#xff0c;即對于一些罕見或未知類別的樣…

商務俄語學習,柯橋基礎入門教學,千萬別小看俄語中的“что”

1、что до (чего) 至于 例&#xff1a; что до меня, то я не могу согласиться 至于我&#xff0c;我不能同意。 А что до зимовки... Ты приедешь в этом году? 說到冬天和過冬…你今年回來嗎…

在windows筆記本中安裝tensorflow1.13.2版本的gpu環境2

tensorflow1.13.2版本的gpu環境 看python-anacona的安裝只需要看1.1部分即可 目錄 1.1 Anaconda安裝 1.2 tensorflow-gpu安裝 1.3 python編譯器-pycharm安裝 1.1 Anaconda安裝 從鏡像源處下載anaconda&#xff0c;地址&#xff1a;Index of /anaconda/archive/ | 北京…

PTA-6-45 工廠設計模式-運輸工具

題目如下&#xff1a; 工廠類用于根據客戶提交的需求生產產品&#xff08;火車、汽車或拖拉機&#xff09;。火車類有兩個子類屬性&#xff1a;車次和節數。拖拉機類有1個子類方法耕地&#xff0c;方法只需簡單輸出“拖拉機在耕地”。為了簡化程序設計&#xff0c;所有…

基于docker實現JMeter分布式壓測

為什么需要分布式&#xff1f; 在工作中經常需要對一些關鍵接口做高QPS的壓測&#xff0c;JMeter是由Java 語言開發&#xff0c;沒創建一個線程&#xff08;虛擬用戶&#xff09;&#xff0c;JVM默認會為每個線程分配1M的堆棧內存空間。受限于單臺試壓機的配置很難實現太高的并…

LeetCode59.螺旋矩陣

LeetCode59.螺旋矩陣 1.問題描述2.解題思路3.代碼 1.問題描述 給你一個正整數 n &#xff0c;生成一個包含 1 到 n2 所有元素&#xff0c;且元素按順時針順序螺旋排列的 n x n 正方形矩陣 matrix 。 示例 1&#xff1a; 輸入&#xff1a;n 3 輸出&#xff1a;[[1,2,3],[8,9,…

Codeforces Round 822 (Div. 2)(D前綴和+貪心加血量)

A.選三條相鄰的邊遍歷一次求最小值 #include<bits/stdc.h> using namespace std; const int N 1e610,mod1e97; #define int long long int n,m; vector<int> g[N]; int a[N]; void solve() {cin>>n;int res2e18;for(int i1;i<n;i) cin>>a[i];sort…

談一談什么是接口測試?怎樣做接口測試?

掃盲內容&#xff1a; 1.什么是接口&#xff1f; 2.接口都有哪些類型&#xff1f; 3.接口的本質是什么&#xff1f; 4.什么是接口測試&#xff1f; 5.問什么要做接口測試&#xff1f; 6.怎樣做接口測試&#xff1f; 7.接口測測試點是什么&#xff1f; 8.接口測試都要掌…

童裝店鋪如何通過軟文增加客流量

在信息超負載、媒介粉塵化、產品同質化多重因素下&#xff0c;傳統營銷疲態盡顯、日漸式微&#xff0c;很難支撐新環境下品牌和企業的持續增長。聚焦童裝行業更是如此&#xff0c;一方面用戶迭代速度快&#xff0c;另一方面&#xff0c;新時代父母的育兒觀念更加精細化&#xf…

安裝pytorch

cuda≤11.6&#xff0c;觀察控制面板 觀察torch對應cuda版本 https://download.pytorch.org/whl/torch/ 安裝cuda11.6.0 CUDA Toolkit Archive | NVIDIA Developer cmd輸入nvcc -V 編輯國內鏡像源 .condarc anaconda prompt輸入 查看環境 conda env list 安裝py3.9…

MySQL面試,MySQL事務,MySQL鎖,MySQL集群,主從,MySQL分區,分表,InnoDB

文章目錄 數據庫-MySQLMySQL主從、集群模式簡單介紹1、主從模式 Replication2、集群模式3、主從模式部署注意事項 UNION 和 UNION ALL 區別分庫分表1.垂直拆分2、水平拆分 MySQL有哪些數據類型1、整數類型**&#xff0c;2、實數類型**&#xff0c;3、字符串類型**&#xff0c;4…