JAVAProcessControl
Scanner
凡是属于I/O流的类如果不关闭就会一直占用资源。
要养成用完就关的习惯。
next方式接收。
next() 不能得到带有空格的字符。
出现空格后,后面的都去掉。
public static void main(String[] args) {
//next() 不能得到带有空格的字符。
        //创建一个扫描器对象,用于接收键盘数据。
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
        if (scanner.hasNext()==true){
            //使用next方式接收
            String str = scanner.next();
            //程序会等待用户输入完毕。
            System.out.println("输出内容为:"+str);
        }
        //凡是属于I/O流的类如果不关闭会一直占用资源。
        //要养成好习惯用完就关。
        scanner.close();
}
运行结果
使用next方式接收:
asas dad
输出内容为:asas
nextline接收方式。
nextline() 返回值是回车之前的所有字符。
public static void main(String[] args) {
//nextline() 返回值是回车之前的所有字符
        
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextline方式接收:");
        //判断是否还有输入
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出内容是:"+str);
        }
        scanner.close();
}
运行结果
使用nextline方式接收:
sd  fr far    r fr 
输出内容是:sd  fr far    r fr 
    
scanner可以不用if语句
public static void main(String[] args) {
    //键盘输入数据
    Scanner scanner = new Scanner(System.in);
    System.out.println("使用nextline方式接收:");
    String str = scanner.nextLine();//输入数据 回车结束
    System.out.println("输出内容是:"+str);
    scanner.close();
}
实例
求和,求平均值。
public static void main(String[] args) {
//输入多个数字,求总和、平均数。每个输入数字用回车确认。
// 通过输入非数字来结束输入并执行结果。
    Scanner scanner = new Scanner(System.in);
    //和
    double sum=0;
    //计算输入了多少个数字。
    int m=0;
    //通过循环判断是否还有输入。
    // 并在里面每一次进行求和和统计。
    while (scanner.hasNextDouble()){
        double x = scanner.nextDouble();
        m =m+1;
        sum = sum +x;
    }
    System.out.println(m+"个数的和为"+sum);
    System.out.println(m+"个数的平均值为"+(sum/m));
    scanner.close();
}
判断小数还是整数
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();
}
运行结果1
请输入整数:
52
整数数据:52
请输小数:
1.3
小数数据:1.3
    
运行结果2
请输入整数:
1.3
输入的不是整数数据!
请输小数:
小数数据:1.3   
顺序结构
依次执行
System.out.println("aaa1");
System.out.println("aaa2");
System.out.println("aaa3");
System.out.println("aaa4");
if结构
if单选择结构
if(判断条件){
满足条件时的下一步}
equals:判断字符串是否相等。
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入内容:");
    String s=scanner.nextLine();
    //equals:判断字符串是否相等。
    if (s.equals("hello")){
        System.out.println(s);
    }
    System.out.println("end");
    scanner.close();
}
if双选择结构
if(判断条件){
满足条件时的下一步}
else{
不满足条件时的下一步}
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入成绩:");
    int score=scanner.nextInt();
    if (score>=60){
        //布尔表达式值为true。
        System.out.println("及格!");
    }else {
        //布尔表达式值为flase。
        System.out.println("不及格!");
    }
    scanner.close();
}
if多选择结构
if(判断条件1){
满足条件1时的下一步}
else if(判断条件2){
满足条件2时的下一步}
else if(判断条件3){
满足条件3时的下一步}
else{
不满足以上条件时的下一步}
public static void main(String[] args) {
/*
if语句至多有一个else
 */
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入成绩:");
    int score = scanner.nextInt();
    if (score == 100){
        System.out.println("恭喜满分!");
    }
    else if (score<100 && score >60){
        System.out.println("及格!");
    }
    else if (score>0 && score<61){
        System.out.println("不及格!");
    }
    else{
        System.out.println("成绩不合法!");
    }
    scanner.close();
}
Switch多项选择结构
switch (表达式){
case 常量1:
代码块1;
break;
case 常量2:
代码块2;
break;
default:
代码块3;
break;}
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
变量类型可以是byte,short,int,long
case穿透,即不加break会继续下面的输出。加入break才会跳出switch语句。
char grade = 'A';
switch (grade){
    case 'A':
        System.out.println("优秀!");
        break;//不加break会继续下面的输出,
    //加上break,满足本条件后输出完会跳出switch。
    case 'B':
        System.out.println("良好!");
        break;
    case 'C':
        System.out.println("及格!");
        break;
    case 'D':
        System.out.println("再接再厉!");
        break;
    case 'E':
        System.out.println("挂科!");
        break;
    //case中没有符合的条件后执行defalt。
    //如果没有defalt,不符合就无输出
    default:
        System.out.println("不符合");
运行结果
    优秀!
循环结构
避免死循环,尽量让循环可以停止下来。
while
while(布尔值表达式){
循环体}
布尔值表达式为false则不进入循环。
计算1+2+...+100
public static void main(String[] args) {
    //避免死循环,尽量让循环可以停止下来。
//  计算1+2+...+100
    int i=0;
    int sum=0;
    //不满足则不进入循环
    while (i<100){
        i++;
        sum = sum +i;
    }
    System.out.println(sum);
}
运行结果
    5050
do while
do{
循环体}while(布尔值表达式)
do while至少循环一次
即先进入循环体,然后判断true or false,以此决定是否继续运行
public static void main(String[] args) {
    //至少循环一次。
    int i=0;
    int sum=0;
    do {
        sum =sum +i ;
        i++;
    }while (i<=100);
    System.out.println(sum);
}
运行结果
    5050   
while与do while比较
public static void main(String[] args) {
    int i=0;
    while (i<0){
        System.out.println(i);
        i++;
    }
    System.out.println("============================");
    do{
        System.out.println(i);
        i++;
    }while (i<0);
    运行结果
============================
0
输出是分割线下面的说明while没输出,只输出了do while。
For循环
for(初始值;布尔值判断;diedai){
}
支持迭代,最有效,最灵活的循环结构。
输出1到100
for (int a=1;a<=100;a++){
    System.out.println(a);
}
System.out.println("for循环结束!");
练习1,计算0到100之间的奇数和偶数的和
public static void main(String[] args) {
    //练习1,计算0到100之间的奇数和偶数的和
    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);
}
练习2,用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i <= 1000; i++) {
    if (i%5==0){
        System.out.print(i+"\t");//print不换行
    }
    if (i%(5*3)==0){//每行
        System.out.println();
    }
}
i%(15)
能整除15时,换行
print输出后不换行
println输出后换行
System.out.print("11");
System.out.print("33");
System.out.println("22");
System.out.print("44");
System.out.print("55");
System.out.print("66");
System.out.println("77");
System.out.print("88");
运行结果
113322
44556677
88
九九乘法表
public static void main(String[] args) {
    for (int j = 1; j <= 9; j++) {
        for (int i = 1; i <= j; i++) {
            System.out.print(i+"*"+j+"="+(i*j)+"\t");
        }
        System.out.println();
    }
}
运行结果
   
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
\t 一个tab键
增强for
for(声明语句:表达式)
{
代码句子
}
int[] numbers = {10,20,30,40,50};//定义一个数组
//便利数组的元素
for (int x:numbers){
    System.out.println(x);
}
上面的语句相当于
int[] numbers = {10,20,30,40,50};//定义一个数组
for (int i = 0; i < 5; i++) {
    System.out.println(numbers[i]);
}
break,continue
break
强制退出循环,但不影响后面的输出。
public static void main(String[] args) {
    int i=0;
    while (i<100){
        System.out.println(i);
        i++;
        if (i==5){
            break;
        }
    }
    System.out.println("123");
}
运行结果
    0
    1
    2
    3
    4
    123
continue
用于终止某次循环
即跳过循环体尚未执行的语句,接着进行下一次是否执行循环的判定
public static void main(String[] args) {
    int i=0;
    while (i<5){
        i++;
        if (i%2==0){
            System.out.println();
            continue;
        }
        System.out.println(i);
    }
}
运行结果
1
3
5
Label
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+"  ");
    }
}
运行结果
101  103  107  109  113  127  131  137  139  149      
练习,打印三角形
public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
        for (int j = 5; j >= i; j--) {
            System.out.print(" ");
        }
        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }
        for (int j = 2; j <= i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
    }
运行结果
     *
    ***
   *****
  *******
 *********
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号