Java的输入输出(含快速输入输出)

1.主类的名字

由于在蓝桥杯当中主类的名字必须是Main,因此也可以在平时的练习当中习惯性的写成Main。
例如下:

public static void main(String[] args){
}

2.输入输出

2.1输入

(1)使用Scanner进行输入

需要定义一个可以在控制台接收键盘输入数据的Scanner对象

Scanner sc=new Scanner(System.in);

然后使用这个Scanner对象配合sc.next…()方法接收数据:

不同类型的数据使用不同的方法。

int a=sc.nextInt();
double b=sc.nextDouble();
long c=sc.nextLong();

字符串需要使用:

sc.next();
或者
sc.nextLine();

in.next() 从缓冲区接收字符遇到空格后停止。

in.nextLine() 从缓冲区接收字符,并且接收空格,遇到换行才停止,并且会自动舍弃换行。

区别:

next():

1、一定要读取到有效字符后才可以结束输入。
2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。

nextLine():

1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。

注意:

如果在nextLine(),之前有其他的输入的话(不包含nextLine(),也就是说2个nextLine()不会出现这个问题),nextLine()会无法输入,原因是:nextLine()会读取上一个输入的回车,解决方法是:加多一个nextLine()来读取上一次的回车即可;有点类似c++的getchar()来读取上一个的回车。

(2)hasNext()的方法

sc.hasNext用法:

sc.hasNext()的返回值是bool值,作用是当在缓冲区内扫描到字符时,会返回true, 否则会发生阻塞,等待数据输入。

实现多组输入可以使用while(sc.hasNext()),相当于C++当中的while(cin>>)

按CTRL+Z结束

用例:

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		
		int a,b,c;
		while(sc.hasNext()){
			a=sc.nextInt();
			b=sc.nextInt();
			c=sc.nextInt();
			System.out.println("输出:"+a+" "+b+" "+c);
		}
		sc.close();
		System.out.println("结束输入");
	}

运行结果:

在这里插入图片描述
针对不同的类型方法也不同:

in.hasNext() // 判断缓存区中还有没有数据,有返回true, 否则等待输入。 in.hasNextInt() //
判断输入的是不是int型的数据,是的话返回true 否则继续扫描缓冲区,或者等待输入。 in.hasNextDouble() //
判断输入的是不是double型的数据,是的话返回true 否则继续扫描缓冲区,或者等待输入。

2.2输出

Java常用的几种输出函数:

System.out.printf(); //和C/C++中的printf一样。 可使用格式控制符进行格式化输出。
// 例如: 输出一个int类型变量  System.out.printf("%d",a);
System.out.print() //不能使用格式控制符进行格式化输出,仅输出变量
System.out.println() //不能使用格式控制符进行格式化输出,仅输出变量,但会自动输出一个换行。

2.快速输入输出

使用BufferedReader和BufferedWriter实现快速输入输出

BufferedReader

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

方法主要使用read() 和 readLine()

String s = in.read() // 读入一个字符 可读入空格回车 但不抛弃回车
String s1 = in.readLine(); // 读入一行 可读入空格可读入回车 但会将回车抛弃
string s2[] = in.readLine().Split(" "); // 使用Split通过空格分割读入的一行字符串,存在s2中

需要注意的是 在windows中按一下回车键 一共有两个字符 “\n\r”
而read()只能读取一个字符所以如要要用read来达到吸收回车的目的,需要用两个read();
如果用readLine()的话会将"\n\r"全部吸收 , 所以只需要一个readLine()来吸收回车。

BufferedWriter

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

主要使用 BufferedWriter类中的 write() 类进行输出。 当数据量大的时候一定要使用这个类进行输出,谨记!

需要注意的是 write() 不能直接输出int类型, 因为write(int a) 会输出其对应的ASCii码的字符 ,比如输出 65 会显示 A

int a = 65;
char b = '2';
String c = "3";
 
out.write(a);
out.write("\n");
out.write(b);
out.write("\n");
out.write(c);
out.write("\n");
out.flush();
 
输出:
A
2
3

主要使用以下几种方法:

int a = 65;
 
out.write(a + "\n");
out.write(Integer.toString(a));
out.flush();
 

应用实例:

static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException{
        //测试writr 不能直接输出int类型
    	int a = 65;
        out.write(a);
        out.write("\n");
    	out.write(a + "\n");  // 使用 + 号拼接个字符串 使参数整体为一个字符串 
        out.write(Integer.toString(a)); // 输出a的字符串形式
        out.write("\n");
       
        //测试 read() 和  readLine();
        int b = in.read();   // read()只读取一个字符
        int c = in.read();   // 吸收 \n
        int x = in.read();   // 吸收 \r
       // String e = in.readLine();
        String d = in.readLine();
        out.write("\n");
        out.write(b + "\n");
        out.write(c + "\n");
        out.write(x + "\n");
        out.write(d + "\n");
        //out.write(e);
        out.flush();
    }
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值