当前位置:   article > 正文

Java | Java的输入与输出_java输入输出

java输入输出

Java输出

1、System.out.println()

System.out.println()可以打印一行内容,输出完自动换行
示例:

public class Inputoutput {
    public static void main(String[] args){
        System.out.println("hello,java!");
        int a=9;
        System.out.println("output:"+a);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

程序输出结果如图所示:
1


2、System.out.printf()

System.out.printf()只能输出一个字符串(Java中的String类型)且不能自动换行,其他类型输出会报错。
示例:

public class Inputoutput {
    public static void main(String[] args){
        String a="wniuniu";
        String b="brilliantgby";
        System.out.printf(a);
        System.out.printf(b);
        System.out.printf("\n");
        System.out.printf("OvO");
        //int b=64;
        //System.out.printf(b);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

程序输出结果如图所示:
2


3、System.out.print()

System.out.print()可以打印字符串以及其他的类型(感觉有些类似python中的print只不过不能自动换行)。
示例:

public class Inputoutput {
    public static void main(String[] args){
        char c='我';//没错,在Java中char有两字节,所以可以存中文
        int a=2187;
        byte b=127;
        System.out.print(c);
        System.out.print('\n');
        System.out.print(a);
        System.out.print("output:"+b);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

程序输出结果如图所示:
3


Java输入

1、使用Scanner类的对象获取输入

为了使用的对象Scanner,我们需要导入java.util.Scanner包。

import java.util.Scanner;
  • 1

然后创建Scanner类对象,使用该对象获取输入。

Scanner input = new Scanner(System.in);
  • 1

应该注意的是,使用完Scanner类对象后应该将其关闭掉,否则会出现警告:Resource leak: 'scanner' is never closed

上述示例中,在main函数末尾使用input.close();即可关闭该对象。

input.close();
  • 1

以下示例都是在创建完Scanner类对象后执行的。

(1)一般类型输入

格式为:

<变量> = input.next变量类型名();
  • 1

注意变量类型首字母的大写。
示例:

import java.util.Scanner;
public class Inputoutput {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int a;
        byte b;
        float f;
        System.out.println("int input:");
        a=input.nextInt();
        System.out.println("output:"+a);

        System.out.println("byte input:");
        b=input.nextByte();
        System.out.println("output:"+b);

        System.out.println("float input:");
        f=input.nextFloat();
        System.out.println("output:"+f);

        input.close();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

运行结果:
4


(2)字符串类型输入

格式为:

<变量> = input.next();
  • 1

上述方法一旦遇到了空格就停止读取了。需要读取一行的话,则使用如下方法:

<变量> = scanner.nextLine();
  • 1

(3)char类型输入

在Scanner类中,char类型的输入没有类似的nextchar()方法。在这里介绍一种输入char类型变量的规则:

<变量>= input.next().charAt(0);
  • 1

示例:

import java.util.Scanner;
public class Inputoutput {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        char c;
        System.out.println("char input:");
        c=input.next().charAt(0);
        System.out.println("output:"+c);
        input.close();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

运行结果:
6


2、使用System.in.read()方法

不建议使用这种方法。
首先要导入IO异常类,然后还要在函数后加上throws IOException,最后是这种方法只能较好地读取char类型变量。

import java.io.IOException;
  • 1

示例:

import java.io.IOException;
public static void main(String[] args) throws IOException {
    char c;
    System.out.print("char input:");
    c = (char) System.in.read();
    System.out.println("Receive char:" + c);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果:
7


更多输入方法及细节可以参考这个博客


END✨

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/491276
推荐阅读
相关标签
  

闽ICP备14008679号