https://www.kuangstudy.com/course
用戶互動Scanner
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
// 創建一個掃描器物件,用于接收鍵盤資料
// IDEA提供了CTRL+ALT+V對該行快速根據變數型別自動生成變數.
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判斷用戶有沒有輸入字串
if(scanner.hasNext()){
// 使用next方式接受
String str=scanner.next();
System.out.println("輸入的內容為:"+str);
}
// 凡是屬于IO流的類如果不關倍訓一直占用資源,要養成好習慣用完就關掉
scanner.close();
}
}
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
// 創建一個掃描器物件,用于接收鍵盤資料
// IDEA提供了CTRL+ALT+V對該行快速根據變數型別自動生成變數
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();
}
}
Scanner進階使用
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 從鍵盤j接收資料
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();
}
}
順序結構
選擇結構
回圈結構
// idea中 100.for 自動生成一個for回圈陳述句
for (int i1 = 0; i1 < 100; i1++) {
}
public class ForDemo1 {
public static void main(String[] args) {
int[] numbers ={10,20,30};
//遍歷陣列的元素
for (int x:numbers){
System.out.println(x);
}
}
}
public class LabelDemo {
public static void main(String[] args) {
// 列印101-150之間所有的質數
int count=0;
// 不建議使用
outer:for (int i=101;i<150;i++){
for (int j=2;j<i/2;j++){
if (i%j==0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/263644.html
標籤:Java
上一篇:【JAVA并發第四篇】執行緒安全
