zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 JAVA开发学习:Scanner 类

    import java.util.Scanner; 
     
    public class ScannerDemo {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            // 从键盘接收数据
     
            // next方式接收字符串
            System.out.println("next方式接收:");
            // 判断是否还有输入
            if (scan.hasNext()) {
                String str1 = scan.next();
                System.out.println("输入的数据为:" + str1);
            }
            scan.close();
        }
    }
    import java.util.Scanner;
     
    public class ScannerDemo {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            // 从键盘接收数据
     
            // nextLine方式接收字符串
            System.out.println("nextLine方式接收:");
            // 判断是否还有输入
            if (scan.hasNextLine()) {
                String str2 = scan.nextLine();
                System.out.println("输入的数据为:" + str2);
            }
            scan.close();
        }
    }
    import java.util.Scanner;
     
    public class ScannerDemo {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            // 从键盘接收数据
            int i = 0;
            float f = 0.0f;
            System.out.print("输入整数:");
            if (scan.hasNextInt()) {
                // 判断输入的是否是整数
                i = scan.nextInt();
                // 接收整数
                System.out.println("整数数据:" + i);
            } else {
                // 输入错误的信息
                System.out.println("输入的不是整数!");
            }
            System.out.print("输入小数:");
            if (scan.hasNextFloat()) {
                // 判断输入的是否是小数
                f = scan.nextFloat();
                // 接收小数
                System.out.println("小数数据:" + f);
            } else {
                // 输入错误的信息
                System.out.println("输入的不是小数!");
            }
            scan.close();
        }
    }
    import java.util.Scanner;
     
    class ScannerDemo {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
     
            double sum = 0;
            int m = 0;
     
            while (scan.hasNextDouble()) {
                double x = scan.nextDouble();
                m = m + 1;
                sum = sum + x;
            }
     
            System.out.println(m + "个数的和为" + sum);
            System.out.println(m + "个数的平均值是" + (sum / m));
            scan.close();
        }
    }
  • 相关阅读:
    144环形链表
    83. 删除排序链表中的重复元素
    21合并两个有序链表
    PyCharm2020激活破解教程
    Python正课目录
    2条pip命令解决Python项目依赖的导出和导出
    pip离线安装模块
    Python正课149 —— luffy项目 User表的配置
    Python正课148 —— luffy项目 数据库配置
    解决:django中LookupError No installed app with label 'admin'
  • 原文地址:https://www.cnblogs.com/tszr/p/10962984.html
Copyright © 2011-2022 走看看