public class Square3{
public static void main(String args[]){
System.out.print("请输入一个整数:");
System.out.println(args[0]);
String nStr=args[0];
int n=Integer.parseInt(nStr);
int nn=n*n;
int nnn=n*n*n;
int nnnn=n*n*n*n;
System.out.println(n+"的平方是:"+nn);
System.out.println(n+"的三次方是:"+nnn);
System.out.println(n+"的四次方是:"+nnnn);
}
}
上面的这段代码编译要java Square3 3才能得到结果。
我想问的是,怎么才能让程序先输出“请输入一个整数:”然后我再填一个数字,得到最后的结果。

解决方案 »

  1.   

    用线程的wait()方法就可以了
    还要注意这个程序要做try...catch
      

  2.   

    /*
     * Created on 2006-9-27
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package com.pss.test.sep;import java.io.BufferedReader;
    import java.io.InputStreamReader;import com.pss.util.prints.Conica;/**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Test2 { public static void main(String[] args) throws Exception{
    Conica.pl("Please input a number, then press Enter..");
    BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));

    String s = keyin.readLine();
    try {
    int n = Integer.parseInt(s);
    System.out.println(n + "的平方是:" + (int)Math.pow(n, 2));
    System.out.println(n + "的三次方是:" + (int)Math.pow(n, 3));
    System.out.println(n + "的四次方是:" + (int)Math.pow(n, 4));
    } catch (Exception ex) {
    Conica.pl("Invalid number.");
    }
    Conica.pl("I love java!");
    }
    }