如何输入字符串,并输出或者赋值给变量

解决方案 »

  1.   

    Scanner sc = new Scanner(System.in);
    String str = sc.next();
    System.out.println(str);
      

  2.   

    InputStreamReader is = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(is);
    try{
    System.out.println(br.readLine());
    }catch(Exception e){}
      

  3.   

    import java.io.*;class Test {
    public static void main(String args[]) {
      char ch[]=new char[60];
      int i=0,j;
      System.out.print("please input:");
             /*请输入字符串,直到回车结束*/
               while( (ch[i]=(char)System.in.read())!='\n')
               i++;
             /*输出字符串*/
    for(j=0;j<=ch.length-1;j++)
      System.out.print(ch[j]);

    }
    }
      

  4.   

    忘了一句:main 函数后面要加 throws IOException,即 public static void main(String args[]) throws IOException,需要抛出异常,否则会提示错误