资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java二元一次方程代码 java二元一次方程代码大全

java 解二元一次方程的程序

import java.util.*;

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网络空间、营销软件、网站建设、米易网站维护、网站推广。

public class T5 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

double a,b,c;

double x1,x2;

System.out.print("请输入a:");

a = in.nextInt();

System.out.print("请输入b:");

b = in.nextInt();

System.out.print("请输入c:");

c = in.nextInt();

x1 = ((-b)+Math.sqrt(b*b-4*a*c))/(2*a);

x2 = ((-b)-Math.sqrt(b*b-4*a*c))/(2*a);

System.out.print("x1="+x1+"\tx2="+x2);

}

}

java 有难度二元一次 编程

import java.io.*;

public class Test {

static String lineTmp;

static String lineEle[];

static float a, b, c;

static int indexOfEqual;

String getInputName() {return getClass().getResource("input.txt").getPath();}

String getOutputName() {return getClass().getResource("input.txt").getPath().replace("input.txt", "result.txt");}

public static void main(String[] args) throws IOException {

Test test = new Test();

FileReader fr = new FileReader(test.getInputName());

BufferedReader br = new BufferedReader(fr);

FileOutputStream fo = new FileOutputStream(new File(test.getOutputName()));

while((lineTmp = br.readLine()) != null) {

lineEle = lineTmp.split(" ");

a = b = c = 0;

for(int i = 0; i lineEle.length; ++i)

if(lineEle[i].equals("=")) indexOfEqual = i;

try {

for(int i = 0; i lineEle.length; ++i) {

if(lineEle[i].contains("x^2")!lineEle[i].contains("+"))

a = Integer.parseInt(lineEle[i].substring(0, lineEle[i].indexOf('x')));

else if(lineEle[i].contains("x")!lineEle[i].contains("+"))

b = Integer.parseInt(lineEle[i].substring(0 ,lineEle[i].indexOf('x')));

else if(!lineEle[i].contains("=")!lineEle[i].contains("+")iindexOfEqual)

c = Integer.parseInt(lineEle[i]);

}

} catch (NumberFormatException e) {fo.write("error\r\n".getBytes()); continue;}

if(a == 0) {

if (b == 0 c != 0) fo.write("x 没有实数根\r\n".getBytes());

else if (b == 0 c== 0) fo.write("R\r\n".getBytes());

else if (b != 0) fo.write(("一个实根:x = " + (-c/b) + "\r\n").getBytes());

} else {

double tmp = Math.pow(b, 2) - 4 * a * c;

if (tmp 0) fo.write(("两实根:x = " + ((-b + Math.sqrt(tmp))/(2 * a)) + ", " +

((-b - Math.sqrt(tmp))/(2 * a)) + "\r\n").getBytes());

else if (tmp == 0) fo.write(("一个实根:x = " + (-b/(2 * a)) + "\r\n").getBytes());

else {

String resultTmp = a 0 ? String.valueOf(Math.sqrt(-tmp)/(2 * a)) : String.valueOf(-Math.sqrt(-tmp)/(2 * a));

fo.write(("两个虚根:x = " + -b/(2 * a) + " + " + resultTmp + "i" + ", " +

-b/(2 * a) + " - " + resultTmp + "i"+ "\r\n").getBytes());

}

}

}

}

}

测试没有问题,输入在input.txt下,放在工程目录src文件夹中即可,输出到同目录下的result.txt中,上面的输入会得到如下输出:

两实根:x = -0.21922359359558485, -2.2807764064044154

两个虚根:x = 0.375 + 1.4523687548277813i, 0.375 - 1.4523687548277813i

R

一个实根:x = -0.75

x 没有实数根

error

不过不知道你是不是要求计算出来,还是写表达式?

怎么用java解二元一次方程?

这是个二元一次方程 解出的结果应该是 x=1 y=1 代码可以这样写 用的方法是穷举。 为了节省程序运行时间 在这里把X和Y的取值定在了10以内 其实多了也没用 答案只有可能是1 。 穷举会按程序的意思去一一例举 占用时间较长 。 代码如下: public class abc { public static void main(String args[]) { for(int x=0;x10;x++) /*定义X取值*/ for(int y=0;y10;y++) /*定义Y取值*/ { if(x+y==2x*y==1) /*定义条件公式*/ System.out.println("x="+x+" y="+y); /*输出结果*/ } } }

怎样用java觖二元一次方程

实现思路就是将满足条件的所有的条件进行循环判断,当满足条件的时候直接跳出循环。

public static void main(String[] args) throws IOException {

double x = 0;

double y = 0;

for(double i = 0; i 2; i = i + 1){

x = i;

y = 2 - x;

if(条件){//此条件就是二元一次方程的算式,可以通过符进行连接

System.out.println(x);

break;

}

}

}

备注:此方法有具有局限性,如果是两组值的话,只能获取一组,所以java的开发有些时候也不是所有的功能都能很方便实现的。


当前标题:java二元一次方程代码 java二元一次方程代码大全
网站地址:http://cdkjz.cn/article/hpojpg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220