📄 roots.java
字号:
import java.io.*;
class InputData // 定义从键盘输入数据的类
{
static private String s="";
static public void input() // 从键盘输入一行字符,保存在字符串s中
{
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in));
try{
s=br.readLine();
}catch(IOException e){} // 捕获输入输出异常
}
static public float getFloat() // 静态方法可直接用类名来调用
{
input();
return Float.parseFloat(s); // 将数字字符串s转化成浮点型数据后返回
}
}
class Equation //表示方程式的类
{
float a,b,c,disc=0;
void input()
{
System.out.println("请输入3个参数a,b,c:");
System.out.print("a=");
a=InputData.getFloat();
System.out.print("b=");
b=InputData.getFloat();
System.out.print("c=");
c=InputData.getFloat();
}
void getRoots() // 计算方程的根
{
double x1=0, x2=0;
double realpart=0, imagpart=0;
if (Math.abs(a)<=1e-5)
{
System.out.println("此方程式不是一元二次方程式。");
System.exit(0);
}
else
{
System.out.print("一元二次方程式");
disc=b*b-4*a*c;
}
if(Math.abs(disc)<=1E-5)
System.out.println("有两个相等的根:"+(-b)/(2*a));
else if(disc>1E-5)
{
x1=(-b+Math.sqrt(disc))/(2*a);
x2=(-b-Math.sqrt(disc))/(2*a);
System.out.println("有两个不相等的根:"+"\nx1="+x1+" x2="+x2);
}
else
{
realpart=-b/(2*a);
imagpart=Math.sqrt(-disc)/(2*a);
System.out.println("有两个复数根:");
System.out.print("x1="+realpart+"+"+imagpart+"i");
System.out.print("\nx2="+realpart+"-"+imagpart+"i");
}
}
}
public class Roots{
public static void main(String[ ] args) throws IOException {
Equation e=new Equation();
e.input();
e.getRoots();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -