1 命令行输入输出

1.1 好用的输入

使用 java.util.Scanner 类进行输入

  • nextInt() 方法获得下一个 int
  • nextDouble() 方法获得下一个 double
  • next() 获得下一个单词

    1. import java.util.Scanner;
    2. class ScannerTest {
    3. public static void main(String args[]) {
    4. Scanner scanner = new Scanner(System.in); // 写法是固定的
    5. System.out.print("请输入一个整数:");
    6. int a = scanner.nextInt();
    7. System.out.printf("%d 的平方是 %d\n", a, a*a);
    8. }
    9. }

    使用 System.out 中的方法进行输出:

  • println() 打印一行(自动换行)

  • print() 打印指定内容(不会自动换行)
  • printf() 格式化输出,与 C 语言中的一样


1.2 读入一个字符

java.io

  • System.in.read()
  • System.out.print() & println() & printf()
    1. char c = ' ';
    2. System.out.print("Please input a char: ");
    3. try {
    4. c = (char)System.in.read();
    5. } catch (IOException e) {
    6. }
    7. System.out.println("You have entered: " + c);

1.3 读入一行 & 读入数字

  • 输入一行
    1. import java.io.*
    2. public class AppLineInOut {
    3. public static void main(String args[]) {
    4. String s = "";
    5. System.out.print("Please input a line:");
    6. try {
    7. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    8. s = in.readLine();
    9. } catch (IOException e) {
    10. }
    11. System.out.println("You have entered: " + s);
    12. }
    13. }

    System.in 一次只能读一个字符,通过流封装后可以输入一个字符串

  • 输入数字

    • Integer.parseInt(s);
    • Double.parseDouble(s);
      1. import java.io.*
      2. public class AppNumInOut {
      3. public static void main(String args[]) {
      4. String s = "";
      5. int n = 0;
      6. double d = 0;
      7. try {
      8. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      9. System.out.print("Please input an int: ");
      10. s = in.readLine();
      11. n = Integer.parseInt(s);
      12. System.out.print("Please input a double: ");
      13. s = in.readLine();
      14. d = Double.parseDouble(s);
      15. } catch (IOException e) {
      16. }
      17. System.out.println("You have entered: " + n + " and " + d);
      18. }
      19. }

      2 图形界面输入输出

  • add(xxxx) 加入对象

  • btn.addActionListener() 处理事件
  • actionPerformed() 具体处理事件 ```java import java.awt.; import java.awt.event.; import javax.swing.*;

public class AppGraphInOut { public static void main( String args[] ) { new AppFrame(); } }

class AppFrame extends JFrame { // 在 swing 中实现 JTextField in = new JTextField(10); JButton btn = new JButton(“求平方”); JLabel out = new JLabel(“用于显示结果的标签”);

  1. public AppFrame() {
  2. setLayout( new FlowLayout() ); // 流式布局,一行一行的放
  3. getContentPane().add( in );
  4. getContentPane().add( btn );
  5. getContentPane().add( out );
  6. btn.addActionListener( new BtnActionAdapter() );
  7. setSize( 400,100 );
  8. setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 设置关闭按钮的动作
  9. setVisible(true);
  10. }
  11. class BtnActionAdapter implements ActionListener {
  12. public void actionPerformed( ActionEvent e ) {
  13. String s = in.getText();
  14. double d = Double.parseDouble( s );
  15. double sq = d * d;
  16. out.setText( d + "的平方是:" + sq );
  17. }
  18. }

}

  1. Java8 中可以简写为 `e->{...}`
  2. ```java
  3. btn.addActionListener(e->{
  4. String s = in.getText();
  5. double d = Double.parseDouble(s);
  6. double sq = Math.sqrt(d);
  7. out.setText(d + " 的平方根是:" + sq);
  8. });