流程控制
Scanner對象
-
之前我們學的基本語法中我們並沒有實現程序與人的交互,但是java給我們提供了這樣的一個工具類,我們可以獲取用戶的輸入。java.until.Scanner 是java5 的新特徵。我們可以通過Scanner類來獲取用戶的輸入。
-
基本語法
Scanner s = new Scanner(System.in);
-
通過scanner類的next()與nextLine()方法獲取輸入的字符串,在讀取前,我們一般需要使用hasNext()與hasNextLine()判斷是否還有輸入的數據。
Scanner對象1
next()
-
一定要讀取到有效字符後纔可以結束輸入。
例如 HELLO WORD 只能活的到HELLO
-
對輸入有效字符之前遇到的空白,next()方法會自動將其去掉。
-
只有輸入有效字符後將其後面的空白作爲分隔符或結束符
-
next()不能得到帶有空格的字符串。
nextLine()
- 以Enter爲結束字符,也就是說nextLine()方法返回的是輸入回車之前的所有字符。
- 可以獲得空白
代碼部分
demo01
package com.luan.scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//創建一個掃描器的對象,用於接收鍵盤數據
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判斷用戶有沒有收入字符串
if(scanner.hasNext()){
String str = scanner.next();
System.out.println("輸出的內容爲:"+str);
}
//凡是使用io流的類,如果不關閉會一直佔用資源,要養成良好的習慣,用完就關掉
scanner.close();
}
}
demo02
package com.luan.scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//創建一個掃描器對象,用於接收鍵盤數據
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine()方式接收:");
//判斷用戶有沒有輸入字符串
if(scanner.hasNextLine()){
//使用next方式接收
String str = scanner.nextLine();
//程序會等待用戶輸入完畢
System.out.println("輸入的內容:"+str);
}
//凡是輸入屬於io流的是如果不關閉一直佔用資源,要量程良好的資源,用完就關閉
scanner.close();
}
}
demo03
package com.luan.scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//創建一個掃描器對象,用於接收鍵盤數據
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入數據:");
//使用next方式接收
String str = scanner.nextLine();
//程序會等待用戶輸入完畢
System.out.println("輸入的內容:"+str);
//凡是輸入屬於io流的是如果不關閉一直佔用資源,要量程良好的資源,用完就關閉
scanner.close();
}
}
demo04
package com.luan.scanner;
import java.util.Scanner;
public class Demo04 {
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();
}
}
demo05
package com.luan.scanner;
import java.util.Scanner;
public class Demo05 {
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();
System.out.println("你輸入了"+m+"個數據,當前結果爲sum="+sum);
m = m + 1 ;
sum = sum + x;
}
System.out.println(m+"個數的和爲"+sum);
System.out.println(m+"個數的平均值是"+(sum/m));
scanner.close();
}
}
選擇結構
單選擇結構
-
我們很多時候判斷一個東西是否是可行,然後我們纔去執行,這樣一個過程在程序中用if語句來表示
-
語法
if(布爾表達式){ //如果布爾表達式爲true,將執行的語句 }
雙選擇結構
-
那現在有個需求,公司收購一個軟件,成功了,給人支付100萬元,失敗了,自己找人開發,這樣的需求用一個if就搞不定了,我們需要有兩個判斷,需要一個雙選擇結構,所以就有了if-else結構。
- 語法
if(布爾表達式){ //如果布爾表達式爲true,將執行的語句 }else{ //如果布爾表達式爲flase }
多選擇結構
-
真實的情況存在abcd,存在區間多級判斷,比如90-100就是A,80-90就是B,等等。。。在生活中的選擇有時候也不僅僅只有兩個,所以我們需要一個多選擇結構來處理這類問題。
-
語法
if(布爾表達式){ //如果布爾表達式爲true,將執行的語句 }else if(布爾表達式 2){ //如果布爾表達式 2爲true,將執行的語句 }else if(布爾表達式 3){ //如果布爾表達式 3爲true,將執行的語句 }else { //如果以上布爾表達式都不爲true執行代碼 }
嵌套的if結構
-
使用的嵌套的if …else語句是合法的,也就是說你可以在另一個if或者else if語句中使用if或else if語句。你可以像if語句一樣,嵌套else if else。
-
語法
if(布爾表達式){ //如果布爾表達式爲true,將執行的語句 }if(布爾表達式 2){ //如果布爾表達式 2爲true,將執行的語句 }
代碼部分
DEMO01
package com.luan.struct;
import java.util.Scanner;
public class ifDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入內容:");
String s = scanner.nextLine();
//equals:判斷字符串是否相等
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("END");
scanner.close();
}
}
DEMO02
package com.luan.struct;
import java.util.Scanner;
public class IfDemo02 {
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("不合格");
}
scanner.close();
}
}
DEMO03
package com.luan.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
//考試成績大於60就是及格,小於60分就是不及格。
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
// if 語句至多有1個 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("不合格");
}else {
System.out.println("成績不合法");
}
scanner.close();
}
}
- 思考?我們尋找一個數,在1-100之間。