用JAVA编程,求解并输出从键盘输入的浮点数的平方根,

2024-12-21 11:40:33
推荐回答(1个)
回答1:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestFloat
{
public static void main(String[] args)
{
System.out.println("请输入一个数:");
double input = 0;
boolean flag=true;
while(flag)
{
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
input=Double.valueOf(in.readLine());
if(input<0)
{
System.out.println("输入的是负数!");
}
else
flag=false;
}
catch(NumberFormatException e)
{
System.out.println("输入格式有误!请重新输入!");

}catch(IOException e)
{
e.printStackTrace();
}
}
System.out.println("输入的数为:"+input);
System.out.println(input+"的平方根为:"+Math.sqrt(input));

}
}