流程
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);
}