由键盘输入数据,字符串-BufferedReader的用法

范例一:有键盘输入一串字符串,

import java.io.*;


public class bufferedreader


{


 public static void main(String args[]) throws IOException


 {


  String mystring;


  BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));


  System.out.println("input a string");


  mystring=buf.readLine();


  System.out.println("string="+mystring);


 }


}

范例2:有键盘输入数据:

import java.io.*;

 

public class bufferedreader


{


 public static void main(String args[]) throws IOException


 {


  String mystring;


  int myint;


  BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));


  System.out.println("input an integer");


  mystring=buf.readLine();


  myint=Integer.parseInt(mystring);


  System.out.println("the integer is="+myint);


 }


}

范例3:由键盘输入两个整数,并且求和:

import java.io.*;

 

public class bufferedreader


{


 public static void main(String args[]) throws IOException


 {


  String mystring1,mystring2;


  int myint1,myint2;


  BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));//定义一个BufferedReader就可以了。


  System.out.println("please input the first number");


  mystring1=buf.readLine();


  myint1=Integer.parseInt(mystring1);


  System.out.println("please input the second number");


  mystring2=buf.readLine();


  myint2=Integer.parseInt(mystring2);


  
  System.out.println(myint1+"+"+myint2+"="+(myint1+myint2));


 }


}

你可能感兴趣的:(jdk-learn)