zoukankan      html  css  js  c++  java
  • java的输入输出

    0x01:输出流

    java常用的输出语句有下面三种:

    1. System.out.println();//换行打印,输出之后会自动换行
    2.  System.out.print();//不换行打印
    3. System.out.printf();//按格式输出

    0x02:输出示例

    public class test {
    
        public static void main(String []args){
        
        System.out.println(1111);//换行打印,输出后自动换行
        System.out.print(1111);//不换行打印
        System.out.printf("分数是:%d",88);//按格式输出
    
        }
    }

    另外,System.out.printf如果报错:

    The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)

    错误是和JDK的本版相关,具体的原因是Java Compiler中Compiler comlicance level的数值太低。将Compiler comlicance level设置为不小于1.5,重新建立工程即可。

    方法:Project >> Properties >> Java Compiler >> Compiler comlicance level。

    0x03:输入流

    java的输入需要依赖Scanner类:

     import java.util.Scanner;

    如果需要输入,则先声明一个Scanner对象:

    Scanner s = new Scanner(System.in);

    Scanner附属于输入流System.in,声明Scanner对象之后,在输入的时候需要使用next()方法系列指定输入的类型,如输入整数、输入字符串等。

    常用的next()方法系列:       nextInt():输入整数    nextLine():输入字符串     nextDouble():输入双精度数     next():输入字符串(以空格作为分隔符)。

    0x04:输入示例

    import java.util.Scanner;
    
    public class test {
    
        Scanner s = new Scanner(System.in);  // 声明Scanner的一个对象
    System.out.print(
    "请输入名字:"); String name = s.nextLine(); System.out.println(name); System.out.print("请输入年龄:"); int age = s.nextInt(); System.out.println(age); System.out.print("请输入体重:"); double weight = s.nextDouble(); System.out.println(weight); System.out.print("请输入学校:") String school = s.next(); System.out.println(school); s.close(); // 关闭输入流,若没有关闭则会出现警告 } }

    输出如下:

    请输入名字:梁 十 安
    梁 十 安
    请输入年龄:18
    18
    请输入体重:70.5
    70.5
    请输入学校:xxx大学 阿斯顿
    xxx大学

    通过输出,我们可以看到nextLine与next的区别

    参考文章:https://blog.csdn.net/baidu_41666198/article/details/79942661

    ***************不积跬步无以至千里***************

  • 相关阅读:
    Mac安装Git以及Git和Github使用教程
    JMeter 聚合报告之 90% Line 参数说明
    [loj3525]喷泉公园
    dapr微服务
    Online SQL interpreter
    C# Math.Round()的银行家算法
    使用Visual Studio分析dump
    Qt高级——QTestLib单元测试框架
    深度学习:Keras入门(一)之基础篇
    前端使用a标签启动本地.exe程序
  • 原文地址:https://www.cnblogs.com/liangshian/p/11713938.html
Copyright © 2011-2022 走看看