第一种:利用Scanner对象
import java.util.Scanner; public class TestInputOuput { public static void main(String[] args) { System.out.println("请输入:"); Scanner inp = new Scanner(System.in); String str = inp.next(); System.out.println("你输入了:"+str); } }
第二种:利用缓存流和输入流
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestInputOuput { public static void main(String[] args) throws IOException { System.out.println("请输入:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("你输入了:"+br.readLine()); } }
第三种:利用输入框
import javax.swing.JOptionPane; public class TestInputOuput { public static void main(String[] args) { String jop = JOptionPane.showInputDialog(null,"请输入: ","输入框标题",JOptionPane.QUESTION_MESSAGE); System.out.println("你输入了:"+jop); } }
运行之后会弹出一个输入框,在中输入即可。