1. 程式人生 > 遊戲攻略 >《寶可夢朱紫》刷閃光迷你龍點位分享 閃光迷你龍在哪刷

《寶可夢朱紫》刷閃光迷你龍點位分享 閃光迷你龍在哪刷

next()方式
package Scanner;

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()){
String str=scanner.next();//程式會等待使用者輸入完畢
System.out.println("輸出內容為:"+str);

}
//凡是屬於IO流的類如果不關閉就會一直佔用資源,要養成好習慣用完關掉
scanner.close();
}
}

nextLine()方式
package Scanner;

import java.util.Scanner;

public class Demo2 {
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();
}
}

 Scanner物件拓展

通過判斷是否輸入的是不是小數整數

package Scanner;

import java.util.Scanner;

public class Demo3 {
public static void main(String[] args) {
//建立一個掃描器物件用於接收鍵盤資料
Scanner scanner=new Scanner(System.in);


int i=0;
float b=0f;
System.out.println("----請輸入整數:----");
if(scanner.hasNextInt()){
i=scanner.nextInt();
System.out.println("輸出內容是一個整數為:"+i);
}else{
System.out.println("輸出內容不是一個整數!");
}
System.out.println("----請輸入小數:----");

if(scanner.hasNextFloat()){
b=scanner.nextFloat();
System.out.println("輸出內容是一個小數為:"+b);
}else{
System.out.println("輸出內容不是一個小數!");
}

scanner.close();
}
}

我們可以輸入多個數字,並求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果;
package Scanner;

import java.util.Scanner;

public class Demo4 {
public static void main(String[] args) {
//我們可以輸入多個數字,並求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果;
Scanner s=new Scanner(System.in);

//和
double sum=0;
//計算輸入了多少個數字
int m=0;
//通過迴圈判斷是否還有輸入,並在裡面對每一次進行求和統計
System.out.println("請輸入第1個數字");
while (s.hasNextDouble()){
double x=s.nextDouble();
m++;
sum+=x;
System.out.println("請輸入第"+(m+1)+"個數字");

}

System.out.println("輸入數字總和為:"+sum);
System.out.println("一共輸入了"+m+"個數字!");
System.out.println("輸入數字的平均數是:"+(sum/m));

}
}