1.接收用户键盘
常用的方式
Scanner shuru = new Scanner(System.in); //System.in是一个标准的输入流,默认接收键盘的输入
String str = shuru.nextLine();
System.out.println(str);
使用BufferedReader用来接收用户的输入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println(str);
2.for循环的增强方式
*## 习惯使用的for循环
int [] a = {10,95,165,25,60};
//遍历
for(int i = 0;i<a.length;i++) {
System.out.println(a[i]);
}
for循环的增强方式
int [] a = {10,95,165,25,60};
for(int e:a) { //int e代表的是集合或者数组中的每一个元素
System.out.println(e);
}
3.改变输出流输出方向
默认输出到控制台
PrintStream ps = System.out;
ps.println("java");
改变输出方向(例如输出到文本里)
//使用此方法记录日志,输出java到文本中
System.setOut(new PrintStream(new FileOutputStream("E:/123456.txt")));
PrintStream ps = System.out;
ps.println("java");
打开123456.txt可以发现文本中多了java