标签:demo 结果 空格 个数 scanner 通过 class 字符串 print
public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
        //判断用户有没有输入字符串
        if (scanner.hasNextLine()){  //hasNext() 会消除空格
            String s = scanner.nextLine();  //scanner.next 会消除空格
            System.out.println("内容为:"+s);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,所有要养好习惯
        scanner.close();
    }
}
public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
        //判断用户有没有输入字符串
        if (scanner.hasNextLine()){  //hasNext() 会消除空格
            String s = scanner.nextLine();  //scanner.next 会消除空格
            System.out.println("内容为:"+s);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,所有要养好习惯
        scanner.close();
    }
}
public class Demo3 {
    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++;
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,结果为"+sum);
        }
        System.out.println(m + "个数的和为:"+sum);
        System.out.println(m + "个数的平均值为:"+(sum/m));
        scanner.close();
    }
}
标签:demo 结果 空格 个数 scanner 通过 class 字符串 print
原文地址:https://www.cnblogs.com/Jaxopp/p/12785898.html