一、使用标准输入串System.in
//System.in.read()一次只读入一个字节数据,而我们通常要取得一个字符串或一组数字
//System.in.read()返回一个整数
//必须初始化
//int read = 0;
char read = ‘0’;
System.out.println(“输入数据:”);
try {
//read = System.in.read();
read = (char) System.in.read();
}catch(Exception e){
e.printStackTrace();
}
System.out.println(“输入数据:”+read);
二、使用Scanner取得一个字符串或一组数字
System.out.print(“输入”);
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
System.out.println(“输入数据:”+read);
/在新增一个Scanner对象时需要一个System.in对象,因为实际上还是System.in在取得用户输入。Scanner的next()方法用以取得用户输入的字符串;nextInt()将取得的输入字符串转换为整数类型;同样,nextFloat()转换成浮点型;nextBoolean()转换成布尔型。/
三、使用BufferedReader取得含空格的输入
//Scanner取得的输入以space, tab, enter 键为结束符,
//要想取得包含space在内的输入,可以用java.io.BufferedReader类来实现
//使用BufferedReader的readLine( )方法
//必须要处理java.io.IOException异常
BufferedReader br = new BufferedReader(new InputStreamReader(System.in ));
//java.io.InputStreamReader继承了Reader类
String read = null;
System.out.print(“输入数据:”);
try {
read = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(“输入数据:”+read);
下面是用bufferedReader读取一行带空格分隔数据的数据行。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
class Node {
private String name;
private Map<Node, Integer> child = new HashMap<Node, Integer>();
public Node(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<Node, Integer> getChild() {
return child;
}
public void setChild(Map<Node, Integer> child) {
this.child = child;
}
}
public class Main {
// 存放开始节点和目标节点
private static String[] resultS = null;
// 每个string【】存放两个节点之前的路径消耗
private static List<String[]> pathList = new ArrayList<>();
public static void main(String[] args) {
inputData();
for (String string : resultS) {
System.out.println(string);
}
for (String[] s : pathList) {
for (String string : s) {
System.out.print(string);
}
System.out.println();
}
// 获取到数据后,进行创建图
createChart(pathList);
}
private static void createChart(List<String[]> pathList2) {
// TODO Auto-generated method stub
Set<Node> set = new HashSet<>();
for (String[] s : pathList) {
for (int i = 0; i < s.length-1; i++) {
if (set.contains(s[i])) {
}
}
System.out.println();
}
}
private static void inputData() {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// java.io.InputStreamReader继承了Reader类
String read = null;
// System.out.print("输入数据:");
try {
if ((read = br.readLine()) != null) {
resultS = read.split(" ");
}
int i = 0;
while ((read = br.readLine()) != null) {
if ("".equals(read)) {
break;//输入空行在键入回车键退出输入结束
}
pathList.add(read.split(" "));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}