java流程控制

java流程控制

Scanner

通过Scanner类可以获取用户的输入

通过Scanner类的next()与nextLine()方法获取输入的字符串,再读取我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

/*
使用next方式接收:
hello world!
hello
*/
package control;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
        if (s.hasNext()){
            String s1 = s.next();
            System.out.println(s1);
        }

        s.close();//关闭io流,不关会占用资源
    }
}

package control;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
        if (s.hasNextLine()){
            String s1 = s.nextLine();
            System.out.println(s1);
        }
        s.close();//关闭io流,不关会占用资源
    }
}
/*
使用next方式接收:
hello world!
hello world!
*/
package control;

public class Demo5 {
    public static void main(String[] args) {
        //输出1-1000之间能被5整除的数,并每行输出三个
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            if (i%5==0) {
                sum+=1;
                System.out.print(i + "\t");
                if (sum%3 == 0){
                    System.out.print("\n");
                }
            }
        }
    }
}

package control;

public class Demo4 {
    public static void main(String[] args) {
        //计算1+2+3+4+。。。100
        int i = 0;
        int sum = 0;
        while(i<=100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

package control;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        //输入多个数字,求其总数和平均数
        Scanner s = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        int i = 0;
        float f = 0.0f;
        while (s.hasNextDouble()){
            double d = s.nextDouble();
            i++;
            f = (float) (d + f);
            System.out.println(f);
        }
        System.out.println(f);
        System.out.println(f/i);
        s.close();//关闭io流,不关会占用资源
    }
}

package control;

public class Demo6 {
    //打印九九乘法表
    public static void main(String[] args) {

        for (int i = 1;i < 10;i++){
            for (int j = 1;j <= i;j++){
                System.out.print("" + i + " * " + j + " = " + i*j + "\t");
            }
            System.out.println();
        }
    }

}

增强for循环

for(声明语句 : 表达式){

//代码句子

}

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

package control;

public class Demo7 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50,60};
        for (int x: numbers){//遍历数组元素
            System.out.println(x);
        }
    }
}

package control;

public class Demo7 {
    public static void main(String[] args) {
        //打印三角形
        for (int i = 0; i < 5; i++) {
            for (int j = 5;j >=i ;j--){
                System.out.print(" ");
            }
            for (int j = 0;j <= i;j++){
                System.out.print("*");
            }

            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }
}

posted @   小国哥哥  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示