NS美服eShop商店舉行情人節特賣 多款合作遊戲優惠
一、Scanner物件
- 基礎語法中並沒有實現程式和人的互動,但java給我們提供了這樣一個工具,可以獲取使用者的輸入,java.util.Scanner是Java5的新特徵,我們可以通過Scanner類獲取使用者的輸入。
- 基本語法:Scanner s = new Scanner(System.in);
- 通過Scanner類的next()與nextLine()方法獲取輸入的字串,在讀取前我們一般使用hasNext()與hasNextLine()判斷是否還有輸入的資料。
package com.mzq.scanner;
import java.util.Scanner;
public class Dome01 {
public static void main(String[] args) {
//先建立一個掃描物件,用於接收鍵盤資料
Scanner scanner = new Scanner(System.in);//通過System.in接受使用者的輸入,
// 把他封裝成scanner物件
System.out.println("使用next方式接收:");
//判斷使用者有沒有輸入字串
if (scanner.hasNext()==true){ //==true可以省略,預設的
//使用next方式接收
String str=scanner.next();
System.out.println("輸入的內容為:"+str);
}
//凡是屬於IO流的類如果不關閉會一直佔用資源,要養成好習慣,用完要關掉
scanner.close();
}
}
package com.mzq.scanner;
import java.util. Scanner;
public class Dome02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("輸入的內容為:"+str);
}
scanner.close();
}
}
next():
(1). 一定要讀取到有效字元後才可以結束輸入;
(2). 對輸入有效字元之前遇到的空白,next()方法會自動將其去掉;
(3). 只有輸入有效字元後才將其後面輸入的空白作為分隔符或者結束符;
(4). next()不能得到帶有空格的字串;
nextLine():
(1).以Eent為結束符,也就是說nextLine()方法返回的是輸入回車之前的所有字元;
(2).可以獲得空白;
nextLine()用的比較多
package com.mzq.scanner;
import java.util.Scanner;
public class Dome03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入資料:");
String str = scanner.nextLine();
System.out.println("輸入的內容為:"+str);
scanner.close();
}
}
輸入整數,小數
package com.mzq.scanner;
import java.util.Scanner;
public class Dome04 {
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.mzq.scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//輸入多個數字,求其和和平均數,每輸入一個數字用回車鍵嗎,通過輸入非數字來結束並輸出結果
Scanner scanner = new Scanner(System.in);
//定義和
double sum = 0;
//定義輸入次數
int n = 0;
System.out.println("請輸入資料");
//通過迴圈進行判斷是否有輸入,並對其求和
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
n=n+1;
sum = sum + x;
}
System.out.println(n+"個數的和為"+sum);
System.out.println(n+"個數的平均值為"+(sum/n));
scanner.close();
}
}
二、順序結構
-
Java的基本結構就是順序結構,除非特別指明,否則就按順序一句一句的執行;
-
順序結構是最簡單的演算法結構;
-
語句與語句之間,框與框之間是按從上到下順序進行的,它是由若干個依次執行的處理步驟組成,它是任何一個演算法都離不開的一種基本演算法結構。
三、選擇結構
if單選擇結構
-
我們去判斷一個東西是否可行,然後我們才去執行,這樣一個過程我們用if語句來表示;
-
語法:
if(布林表示式){ //如果布林表示式為ture將執行的語句 }
package com.mzq.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();
//判斷字串是否相等
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
if雙選擇結構
-
需要兩個判斷,此時需要一個雙選擇結構,所以就有了if-else結構
-
語法:
if(布林表示式){ //如果布林表示式為ture將執行的語句 }else{ //如果布林表示式為false將執行的語句 }
package com.mzq.struct; import java.util.Scanner; public class IfDemo02 { public static void main(String[] args) { //大於等於60分及格,反之不及格 Scanner scanner = new Scanner(System.in); System.out.println("請輸入一個數"); double v = scanner.nextDouble(); if (v>=60){ System.out.println("及格:"+v); }else { System.out.println("不及格:"+v); } scanner.close(); } }
if多選擇結構
-
不僅僅選擇兩個,所以我們需要多選擇結構;
-
語法:
if(布林表示式 1){ //如果布林表示式為ture1將執行的語句 }else if(布林表示式 2){ //如果布林表示式為ture2將執行的語句 }else if(布林表示式 3){ //如果布林表示式為ture3將執行的語句 }else{ //如果布林表示式都不為ture將執行的語句 }
package com.mzq.struct; import java.util.Scanner; public class IfDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入一個數:"); double v = scanner.nextDouble(); if (v==100){ System.out.println("滿分:"+v); }else if (v<100 && v >=80){ System.out.println("優秀:"+v); }else if (v<80 && v>=60){ System.out.println("及格:"+v); }else if (v<60){ System.out.println("不及格:"+v); }else { System.out.println("輸入成績不符合"); } scanner.close(); } }
if巢狀結構
語法
if(布林表示式 1){
//如果布林表示式為ture1將執行的語句
if(布林表示式 2){
//如果布林表示式為ture2將執行的語句
}
}
Switch選擇結構
-
Switch case語句判斷一個變數與一系列值中某個值是否相等,每個值稱為一個分支;
-
語法:
switch (expression){ case value: //語句 break;//可選 case value: //語句 break;//可選 //可以任意數量的case語句 default://可選 }
-
Switch語句中的變數型別可以是:
byte、short、int、或者char;
從JavaSE7開始,switch支援字串String型別 同時case標籤必須為字串常量或字面量。
四、迴圈結構
while迴圈
while是最基本的迴圈,語法結構為:
while(布林表示式){
//迴圈內容`在這裡插入程式碼片`
}
只要布林表示式為ture,迴圈就會一直執行下去。就會造成死迴圈;
大多數情況下會讓迴圈停下來,需要一個表示式失效的方式來結束迴圈
一些情況下需要迴圈一直執行,比如伺服器的請求響應監聽;
package com.mzq.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//輸出1~100
int i=0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.mzq.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死迴圈 程式中儘量避免
while (true){
}
}
}
1+2=3+,+100
package com.mzq.struct;
public class WhileDemo03 {
public static void main(String[] args) {
int i =0;
int sum=0;
while (i<100){
i++;
sum=sum+i;
}
System.out.println(sum);
}
}
do…while迴圈
對於whlie語句來說,如果不滿足條件,他就不會去執行,do…while迴圈與while迴圈相似,不同的是do…while迴圈至少會執行一次;
語法結構:
do{
//程式碼
}while(布林表示式);
while與do…while的區別:
while先判斷後執行,do…while先執行後判斷;
do…while總是保證迴圈體會至少執行一次;
package com.mzq.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
i++;
sum=sum+i;
}while (i<100);
System.out.println(sum);
}
}
for迴圈
for迴圈是支援迭代的一種結構,是最有效,最靈活的迴圈體結構;
執行迴圈次數提前就確定了,語法:
for(初始值;布林表示式;更新){
//程式碼
}
0~100奇數和偶數的和:
public static void main(String[] args) {
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);
System.out.println("偶數和:"+evenSum);
}
用for迴圈和while迴圈輸出1~1000之間能被5整除的數,並且每行輸出3個
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t"); //\t空格
}
if (i%15==0){
System.out.println();
//System.out.print("\n");
//print輸出不會換行
//println輸出會換行
}
}
}
public static void main(String[] args) {
int i = 1;
while (i<=1000){
if (i%5==0){
System.out.print(i+"\t");
}
if (i%15==0) {
System.out.println();
}
i++;
}
}
99乘法表
public static void main(String[] args) {
for (int i = 1;i <= 9;i++){
for (int j = 1;j <= i;j++){
System.out.print(i+"*"+j+"="+i*j);
System.out.print("\t");
}
System.out.println();
}
}
增強for迴圈
陣列重點使用(用於陣列或集合的增強型for迴圈)。
語法格式如下:
for(宣告語句:表示式){
//程式碼
}
public static void main(String[] args) {
int[] numbers = {10,33,43,42,41} ;//定義一個數組
for (int i = 1;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("==============");
for (int x:numbers){//numbers每一項數值遍歷出來複製給x
System.out.println(x);
}
}
break continue
-
break在任何迴圈語句的主體部分,均可用break控制迴圈的流程,break用於強行退出迴圈,不執行迴圈中剩餘語句。(也在switch語句中使用)
public static void main(String[] args) { int i = 0; while (i<100){ i++; System.out.println(i); if (i==30){ break; } } System.out.println("6666");//break只是跳出迴圈,並未終止 }
-
continue語句用在迴圈語句體中,用於終止某次迴圈過程,即跳過迴圈體中尚未執行語句,接著進行下一次是否執行迴圈的判定。
public static void main(String[] args) { int i = 0; while (i<100){ i++; if (i%10==0){ System.out.println(); continue; } System.out.print("\t"); System.out.print(i); } }
五、練習
列印三角形
package com.mzq.struct;
import java.util.Scanner;
public class TestDemo01 {
public static void main(String[] args) {
System.out.println("請輸入三角形行數(整數!)");
int a=0;
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()){
a = scanner.nextInt();//接收資料
}else{
System.out.println("輸入的不是整數資料");
}
for (int i = 1; i <= a; i++) {
for (int j=a; 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();
}
}
}