Java的流程控制
- 基礎階段 目錄:
- 用戶交互Scanner
- 順序結構
- 選擇結構
- 循環結構
- break & continue
- 練習題
1.Scanner對象
之前我們學的基本語法中並沒有實現程序和人的交互,但是Java給我們提供了這樣一個工具類,我們可以獲取用戶的輸入。Java.util.Scanner是Java5的新特徵,我們可以通過Scanner類來獲取用戶的輸入。
基本語法:
Scanner s = new Scanner(System.in);
通過Scanner類的next()與nextLine()方法獲取輸入的字符串,在讀取前我們一般需要使用hasNext()與hasNextLine()判斷是否還有輸入的數據。
next():
一定要讀取到有效字符後纔可以結束輸入。//必須要輸入否則程序不停止
對輸入有效字符之前遇到的空白,next()方法會自動將其去掉。//Hello World 只輸出Hello因爲有空格
只有輸入有效字符纔將其後面輸入的空白作爲分隔符或者結束符。/’/只有程序中有空格就結束’
next()不能得到帶有空格的字符串。
使用next()的例子:
package com.company.base;
import java.util.Scanner;
public class demo1 {
public static void main(String[] args) {
//創建一個掃描器對象,用於接受鍵盤數據
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判斷用戶有沒有輸入字符串
if (scanner.hasNext()){
//使用next方式接收
String str = scanner.next();
System.out.println("輸出的內容爲:"+str);
}
scanner.close(); //IO流的用完記得關掉 佔用內存 IO流就是輸入輸出流 和電腦交換信息的
}
}
- nextLine()
- 以Enter爲結束符,也就是說nextLine()方法返回的是輸入回車之前的所有字符。
- 可以獲得空白。
package com.company.base;
import java.util.Scanner;
public class demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入數據");
if(scanner.hasNextLine()){
String abc = scanner.nextLine();
System.out.println("輸出:"+abc);
}
scanner.close();
}
}
- 判斷整數與小數的scanner
package com.company.base;
import java.util.Scanner;
public class demo4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//從鍵盤接收數據
int i = 0;
float f = 0.0f;
System.out.println("請輸入整數:");
//如果。。。那麼。。。
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整數數據:"+ i);
}else {
System.out.println("輸入的不是整數數據");
}
System.out.println("請輸入小數");
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小數數據:"+ f);
}else {
System.out.println("輸入的不是小數數據!");
}
scanner.close();
}
}
做一個簡易計算器
package com.company.base;
import java.util.Scanner;
public class demo5 {
public static void main(String[] args) {
//我們可以輸入多個數字,並求其總和與平均數,每一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果:
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//計算輸入了多少個數字
int m = 0;
//通過循環判斷是否還有輸入,並在裏面對每一次進行求和和統計
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m+1;//m++
sum = sum + x;
System.out.println("你輸入了第"+m+"個數據,然後點錢結果sum"+sum);
}
System.out.println(m +"個數的和爲"+ sum);
System.out.println(m+ "個數的平均值是"+(sum / m));
scanner.close();
}
}
2.順序結構
Java的基本結構就是順序結構,除非特別指明,負責就按照順序一句句執行。
順序結構是最簡單的算法結構。
-
語句與語句之間,框與框之間是按從上到下的順序進行的,它是由若干個依次執行的處理步驟組成的,它是任何一個算法都離不開的一種基本的算法結構。
那還沒搞明白 那就給下面的RUN一下你就知道了
package com.company.base;
public class demo6 {
public static void main(String[] args) {
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
System.out.println("6");
System.out.println("7");
System.out.println("8");
}
}
3.選擇結構(很重要的)
- if單選擇結構
- if雙選擇結構
- if多選擇結構
- 嵌套的if結構
- switch多選擇結構
- if單選擇結構
我們很多時候需要去判斷一個東西是否可行,然後我們纔去執行,這樣一個過程在程序中用if語句來表示
語法:
if(布爾表達式){
//如果布爾表達式爲true將執行的語句,否則跳過
}
- if雙選擇結構
- 需要兩個判斷時,就需要一個雙選則結構,所以就有了if-else結構。
- 語法:
if(布爾表達式){
//如果布爾表達式的值爲true
}else{
//如果布爾表達式的值爲false
}
package com.company.ifdemo;
import java.util.Scanner;
public class if2 {
public static void main(String[] args) {
//考試分數大於60就是及格,小於60分就是不及格
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
- if多選擇結構
- 我們發現剛纔的代碼不符合實際情況,真實的情況還可能存在ABCD,存在區間多級判斷。比如90-100就是A,80-90就是B等等。在生活中很多時候我們的選擇也不僅僅就只有這兩個,所以我們需要一個多選擇結構來處理這類問題。
if(布爾表達式1){
//如果布爾表達式1的值爲true執行代碼
}else if(布爾表達式2){
//如果布爾表達式2的值爲true執行代碼
}else if(布爾表達式3){
//如果布爾表達式3的值爲true執行代碼
}else{
//如果以上布爾表達式都不爲true執行代碼
}
package com.company.ifdemo;
import java.util.Scanner;
public class if3 {
public static void main(String[] args) {
//考試分數大於60就是及格,小於60分就是不及格
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
/*
if語句至多有一個else語句,else語句在所有的else if語句之後。
if語句可以有若干個else if語句,它們必須在else語句之前。
一旦其中一個else if 語句檢測爲true,其他的else if以及else語句都將跳過執行
*/
if (score == 100) {
System.out.println("恭喜你 你得到了滿分");
} else if (score < 100 && score >= 90) {
System.out.println("A級");
} else if (score < 90 && score >=80) {
System.out.println("B級");
} else if (score < 80 && score >= 70) {
System.out.println("C級");
}else if (score<70 && score>=60){
System.out.println("D級");
}else if (score<60 && score>=0){
System.out.println("不及格好好學習吧");
}
}
}
4. 嵌套的if結構
* 使用嵌套的if…else語句是合法的。也就是說你可以在另一個if或者else if語句中使用if或者else if語句。你可以像if語句一樣嵌套else if…else。
* 語法
if(布爾表達式1){
//如果布爾表達式1的值爲true執行代碼
if(布爾表達式2){
//如果布爾表達式2的值爲true執行代碼
}
}
4.Switch多選擇結構
- 多選擇結構還有一個實現方式就是switch case語句。
- switch case語句能判斷一個變量與一系列值中某個值是否相等,每個值稱爲一個分支。
- switch語句中的變量類型可以是:
- byte、short、int、或者char
- 從Java SE 7 開始
- switch支持字符串String類型了
- 同時case 標籤必須爲字符串常量或者字面量。
Switch(expression){
case value:
//語句
break;//可選
case value:
//語句
break;//可選
//你可以有任意數量的case語句
default://可選
//語句
}
第一個例子:
package com.company.jiaohuan;
public class JH {
public static void main(String[] args) {
//
char grade = 'A';
switch (grade){
case 'A':
System.out.println("優秀");
break;//可選
case 'B':
System.out.println("良好");
break;//可選
case 'C':
System.out.println("及格");
break;//可選
case 'D':
System.out.println("再接再厲");
break;//可選
case 'E':
System.out.println("拉了兄弟");
break;//可選
default:
System.out.println("未知等級");
}
}
}
第二個例子:
package com.company.jiaohuan;
public class JH2 {
public static void main(String[] args) {
String name = "chb";
//JDK 7 新特性,表達式結果可以是字符串!!!
//字符的本質還是數字!!!
//反編譯 java----class(字節碼文件)---反編譯(IDEA)
switch (name){
case "RAP":
System.out.println("RAP");
break;
case "整活":
System.out.println("整活");
default:
System.out.println("妹有啊");
}
}
}
5.循環結構
- while循環
while(布爾表達式){
//循環內容
}
* 只要布爾表達式爲true,循環就會一直執行下去。
* 我們大多數情況是會讓循環停止下來的,我們需要一個讓表達式失效的方式來結束循環。
* 少部分情況需要循環一直執行,比如服務器的請求響應監聽等。
* 循環條件一直爲true就會造成無限循環【死循環】我們正常的業務編程中應該儘量避免死循環。會影響程序性能或者造成程序卡死崩潰!
package com.company.whiledemo01;
public class Whiledemo1 {
public static void main(String[] args) {
//輸出1-100
int i = 0;
while (i < 5201314){
i++;
System.out.println(i);
}
}
}
計算1+2+。。+100
public class Whiledemo3{
public static void main(String[] args) {
//計算1+2+。。+100
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
do…while循環
對於while語句而言,如果不滿足條件,則不能進入循環。但有時候我們需要即使不滿足條件,也至少執行一次。
do…while循環和while循環相似,不同的是,do…while循環至少會執行一次
do{
//代碼語句
}while(布爾表達式);
- 使用do…while語句計算1+2+。。+100
package com.company.whiledemo01;
public class dowhiledemo1 {
public static void main(String[] args) {
int i = 0 ;
int sum = 0 ;
do{
sum = sum + i;
i++;
}while ((i<=100));
System.out.println(sum);
}
}
public class dowhiledemo2{
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("===========");
do{
System.out.println(a);
a++;
}while (a<0);
}
while和do…while的區別:
while先判斷後執行,dowhile是先執行後判斷!
DO…while總是保證循環體會被至少執行一次!這是他們的主要差別。
for循環
在Java5中引入了一種主要用於數組的增強型for循環