Java語言程式設計(基礎篇)第十版 程式設計練習題3.3(代數: 求解2*2線性方程)
阿新 • • 發佈:2018-12-10
**編寫程式,提示使用者輸入a,b,c,e和f,然後顯示結果。 如果ad-bc=0,則方程式無解 應用Cramer規則求解線性方程組**
public class linearEquation {
public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter a, b, c, d, e, f:"); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double d = input.nextDouble(); double e = input.nextDouble(); double f = input.nextDouble(); //提醒使用者輸入a,b,c,d,e,f double discriminant = a * d - b * c; if(discriminant != 0) { double x = (e * d - b * f) / discriminant ; double y = (a * f - e * c) / discriminant ; System.out.println("x is " + x + " and y is " + y ); } else { System.out.println("The equation has no solution"); System.exit(1);//終止程式 } }
} “`