*3.3(Algebra:solve 2×2linear equations)A linear equation can be solved using Crame's rule given in Programming Exercise 1.13 .Write a program that prompts the user to enter a,b,c,d,e,and,f and displays the result. If ad- bc is 0,report that "The equation has no solution."
*3.3 (代数:解2×2线性方程)线性方程可以用编程练习1.13给出的克莱默法则求解.编写一个提示程序使用户输入a,b,c,d,e和f,并显示结果.如果ad-bc为0,则报告"The equation has no solution."
代码如下:
import java.util.Scanner;
public class Unite3Test3 {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("请输入a的值:");
double a=scan.nextDouble();
System.out.println("请输入b的值:");
double b=scan.nextDouble();
System.out.println("请输入c的值:");
double c=scan.nextDouble();
System.out.println("请输入d的值:");
double d=scan.nextDouble();
System.out.println("请输入e的值:");
double e=scan.nextDouble();
System.out.println("请输入f的值:");
double f=scan.nextDouble();
double x=(e*d-b*f)/(a*d-b*c);
double y=(a*f-e*c)/(a*d-b*c);
if(a*d-b*c==0)
{
System.out.println("The equation has no solution.");
}else
{
System.out.println("x is"+x+"and"+"y is"+y);
}
}
}
运行结果如下: