java 二元一次方程解

tortoises1988 2010-04-03 08:25:33
下面是我写的程序,由于是初学,麻烦帮我看看是什么地方有问题,谢谢XDJM了。
弹出的提示:
Exception in thread "main" java.lang.NumberFormatException:empty String
at sun.mise.FloatingDecimal.readJavaFormatString(Unknown Source)
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
{System.in.read(buf);
}
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);
}
}
}

...全文
605 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
麦穗 2010-04-04
  • 打赏
  • 举报
回复
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
{System.in.read(buf);
}
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
  • 打赏
  • 举报
回复
估计问题出现在parseDouble内~
tortoises1988 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 amdgaming 的回复:]
转化异常 看看 你个string输错了吗?然后 处理一下 异常
[/Quote]
不好意思忘说题目了,题目是:
编写程序,求解一元二次方程ax2+bx+c=0的根。要求a, b, c从键盘输入;程序需考虑a, b, c为任意实数的各种情况。

可不可以具体说说应该怎么改,谢谢。
amdgaming 2010-04-03
  • 打赏
  • 举报
回复
转化异常 看看 你个string输错了吗?然后 处理一下 异常

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧