java 二元一次方程解

tortoises1988 2010-04-03 08:25:33

Exception in thread "main" java.lang.NumberFormatException:empty String
at java.lang.Double.parseDouble(Unknown Source)
at Z1_1.main(Z1_1,java:26)

import java.io.*;
import java.util.Scanner;
import java.lang.Math;
public class Z1_1
{public static void main(String arg[])
{System.out.println("Input three numbers,format:a,b,c");//建议用","分隔
byte buf[]=new byte[4];
try
}
catch(IOException e)
{}
String s=new String(buf);
s=s.trim();
int pos1=s.indexOf(',');
String sa=s.substring(0,pos1);//取第一个数
int pos2=s.lastIndexOf(',');
String sb=s.substring(pos1+1,pos2);//取第二个数
String sc=s.substring(pos2+1);//取第三个数

System.out.println("sa="+sa);
System.out.println("sb="+sb);
System.out.println("sc="+sc);

double a=Double.parseDouble(sa);
double b=Double.parseDouble(sb);
double c=Double.parseDouble(sc);

System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);

if(a==0&b==0)
{System.out.println("无解!");
}

else if(a==0&b!=0)
{double x1=-c/b;
double x2=-c/b;
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}

else if(Math.sqrt(b*b-4*a*c)<0)
{System.out.println("无实数解!");
}

else
{double x1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
double x2=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}
}
}

...全文
609 5 打赏 收藏 转发到动态 举报

5 条回复

• 打赏
• 举报

LZ是一场没有处理，加个try{}catch{},试试！
tortoises1988 2010-04-03
• 打赏
• 举报

``````
import java.io.*;
import java.util.Scanner;
import java.lang.Math;
public class Z1_1
{public static void main(String arg[])
{System.out.println("Input three numbers,format:a,b,c");//建议用","分隔
byte buf[]=new byte[4];
try
}
catch(IOException e)
{}
String s=new String(buf);
s=s.trim();
int pos1=s.indexOf(',');
String sa=s.substring(0,pos1);//取第一个数
int pos2=s.lastIndexOf(',');
String sb=s.substring(pos1+1,pos2);//取第二个数
String sc=s.substring(pos2+1);//取第三个数

System.out.println("sa="+sa);
System.out.println("sb="+sb);
System.out.println("sc="+sc);

double a=Double.parseDouble(sa);
double b=Double.parseDouble(sb);
double c=Double.parseDouble(sc);

System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);

if(a==0&b==0)
{System.out.println("无解!");
}

else if(a==0&b!=0)
{double x1=-c/b;
double x2=-c/b;
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}

else if(Math.sqrt(b*b-4*a*c)<0)
{System.out.println("无实数解!");
}

else
{double x1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
double x2=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}
}
}

``````
run_storm 2010-04-03
• 打赏
• 举报

tortoises1988 2010-04-03
• 打赏
• 举报

[Quote=引用 1 楼 amdgaming 的回复:]

[/Quote]

amdgaming 2010-04-03
• 打赏
• 举报

62,624

• 近7日
• 近30日
• 至今