程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

java 流程控制学习

发布于2021-03-10 18:05     阅读(985)     评论(0)     点赞(23)     收藏(3)


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+" ");

        }

    }
}


所属网站分类: 技术文章 > 博客

作者:zhqu4399

链接:http://www.javaheidong.com/blog/article/112346/8fe5ea6846b17e928b10/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)