从键盘输入两个整数,然后输出他们的平方值及立方值

来源:互联网 发布:淘宝网联名信用卡 编辑:程序博客网 时间:2024/06/05 18:08
/** * 从键盘输入两个整数,然后输出他们的平方值及立方值 *  * java se5以后提供了一个比较好的输入方法下面是一个小例子:import java.util.*;class test{public static void main(String []args){Scanner cin=new Scanner(System.in);  System.out.println("请输入你的名字:");  String name=cin.nextLine();  System.out.println("你输入你的年龄");  int age=cin.nextInt();  System.out.println("你的名字是:"+name+" "+"你的年龄是:"+age);}} */import java.io.*;;class InputData{static private String s = "";/** * 从输入流中获取字符串 */static public void input(){BufferedReader bu = new BufferedReader(new InputStreamReader(System.in)//标准输入流:字节流/** * inpublic static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。 *//** * InputStream 是字节流reader是字符流InputStreamReader是将字节流转换成字符流,是上面两者的转换类。比如输入时inputStram,可以用inputStreamReader(inputStream);返回reader的字符流 *//** * inputstream,只能读字节,byte数组。例如System.in,标准输入流,获得的是字节流 * 而有reader的可以读char,例如inputStreamReader(inputStream)是将字节流转换成字符流 * 而有buffered的可以读string,例如BufferedReader(inputStreamReader)是将字符流转换成字符串 * BufferedReader获取字符串的方法是 readline() */);try {s = bu.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 从输入流中获取字符串,转为整数值 * @return */static public int getInt(){input();return Integer.parseInt(s);}}class Result{void print(int d){System.out.println(d+"的平方:"+d*d);System.out.println(d+"的立方:"+d*d*d);}}public class Test6_4 {public static void main(String[] args) throws FileNotFoundException {// TODO Auto-generated method stubResult result = new Result();System.out.println("请输入一个整数:");int a = InputData.getInt();result.print(a);}}

原创粉丝点击