Java输入的几种情况

package JavaTest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class InputDemo {
	
	public static void main(String[] args) throws IOException{
		//Demo_1();
		
		//Demo_2();
		
		//Demo_3();
		
		Scanner scr=new Scanner(System.in);
		BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\xuan.txt"));
		System.out.println("请输入");
		String name=scr.next();
		System.out.println("result:"+name);
		bw.write(name);
		bw.newLine();
		bw.flush();
		bw.close();
		
	}

	public static void Demo_3() throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
		String line=null;
		while((line=br.readLine())!=null){
			if(line.equals("#")){
				break;
			}
			//System.out.println("result:"+line);
			bw.write("result:"+line+"\r\n");
			//bw.newLine();
			bw.flush();
		}
	}
	
	/
	public static void Demo_2() throws IOException {
		//创建容器
		StringBuilder sb=new StringBuilder();
		//获取键盘读取流
		InputStream in=System.in;
		//定义变量获取读取到的字节,并循环获取
		int ch=0;
		while((ch=in.read())!=-1){
			if(ch=='\r')
				continue;
			if(ch=='\n'){
				String temp=sb.toString();
				if(temp.equals("#")){
				    break;
				}
				System.out.println("输出结果为:"+temp);
				sb.delete(0, sb.length());
			}else{
				sb.append((char)ch);//将读取到的字节储存(并接)起来
				//System.out.println("result:"+ch);
			}
		}
	}
	/

	public static void Demo_1() throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		int len=0;
		while((len=br.read())!=-1){
			System.out.println("键盘输入结果:"+(char)len);
		}
		br.close();
	}
	

}


第一种:(输入整数)

int a=System.in.read();

第二种:(万能输入)

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();


java.util.Scanner sc = new java.util.Scanner(System.in);

int b = sc.nextInt();



public class InputSemo {
	
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入1");
		int num=scanner.nextInt();
		System.out.println("请输入2");
		String string=scanner.nextLine();
		System.out.println("请输入3");
		String str =scanner.next();
		if(num==0){
			System.out.println("请输入");
		}
		if(string==null){
			System.out.println("请输入");
		}
		if(str==null){
			System.out.println("请输入");
		}
		
		System.out.println(num+"=="+string+"=="+str);
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值