1.使用Scanner
Scanner sc = new Scanner(System.in);
如果要输入整数,则 int n = sc.nextInt();
String类型的,则String temp = sc.next();
比如:
import java.util.Scanner;
public class Test {
}
2.使用BufferedReader
用前需要引入 import java.io.Reader;
BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();
比如:
==================================================================================================
import java.io.*;
public class importtext {
public static void main(String[] args) {
String st;
int num;
float fnum;
try{
System.out.print("输入:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
st = br.readLine();
System.out.print("输入一个数:");
num = Integer.parseInt(br.readLine());
System.out.print("输入一个浮点数:");
fnum = Float.parseFloat(br.readLine());
System.out.print("输出:"+st+'\n');
System.out.print("输出:"+num+'\n');
System.out.print("输出:"+fnum+'\n');
}catch(IOException e){}
}
}
==================================================================================================
package com.s2;
import java.io.*;
public class Input
{
}
==================================================================================================
应该注意的是:Java把从键盘输入的数据一律看作是字符串,因此若要从键盘输入并让系统认可是数值型数据,必须经过转换。
比如:
package com.s2;
import java.io.*;
public class Input
{
}