流程
Scanner用戶交互
package com.example.demo.scanner;
import java.util.Scanner;
//對I/O操作時,用完就關閉,否則占用資源。
//8-29 流程控制
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Scannner next is:" + sc.next());//對空白符作為結束符,使用hello world會截斷輸出hello(在遇到有效字符前一直等待)。
//System.out.println("Scanner nextline is :"+sc.nextLine());以回車為結束
double sum = 0.0;
int c = 0;
while (sc.hasNextDouble()) {
sum += sc.nextDouble();
c += 1;
}
System.out.println("The sum is:" + sum + " avg is :" + sum / c);//好奇怪的輸入 int被自動轉換為高精度的double,所以輸入有效。
sc.close();
}
}
流程
//8-30 循環分支流程
/*if(){}else if(){}else{}
switch(expression){case value:break;}
while(){} do{}while() for(){}
* */
/*區別于C等其他
* */
int[] nums = {1, 2, 3};
String[] strings = {"abc", "def", "ghi", "jkl"};
// for (int i=0;i< nums.length;i++){
// System.out.println(nums[i]);
// }
//加強數組循環 先了解
for (int i : nums) {
System.out.println(i);
}
for (String s : strings) {
System.out.println(s);
}