方法一:在控制台接收字符串并将其打印出来,使用BufferedReader和InputStreamReader类实现
import java.io.*; public class Demo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("please input a string"); String str = br.readLine(); System.out.println(str); } }
方法二:使用Scanner类实现
import java.util.Scanner; public class Demo1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("input your name"); String name = sc.nextLine(); System.out.println("input your age"); int age = sc.nextInt(); System.out.println("input you height"); float height = sc.nextFloat(); System.out.println("Your information: name:" + name + " age:" + age + " height:" + height); } }