从键盘输入1000以内正整数,输出其平方根

public static double getSqrt(){
    System.out.println("请输入:");
    Scanner scanner = new Scanner(System.in);
    //1
    //        int n;
    //        do{
    //            n = scanner.nextInt();
    //        }while (!(n < 10000));
    //        return Math.sqrt(n);
    //2
    //        int n = scanner.nextInt();
    //        while (!(n < 1000)){
    //            n = scanner.nextInt();
    //        }
    //        return Math.sqrt(n);
    //3.
    while(true){
        int n = scanner.nextInt();
        if(n <1000 && n>=0) {
            System.out.println(Math.sqrt(n));
        } else {
            System.out.println("请重新输入");
        }
    }
}
public static void main(String[] args) {
    getSqrt();
}}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44827160/article/details/88783204