Java实现简单的克莱姆法则
/**
* @author: Niu Zhaohang
*
* This includes :
* a) Checker for types of values users input by using [try...catch].
* (They can only input numbers or the program will die.)
* b) Counter for times that users input out-ranged numbers by using [do...while].
* (They can only try 3 times or the program will die.)
* c) Checker for types of numbers users input by using [if...else, printf]
* (If they input integers, the program will form the formula by using integers instead of double.)
* d) Calculator for x, y.
**/
import java.util.Scanner;
public class CramersRule {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("ax + by = e , cx + dy = f, ATTENTION: a,b,c,d is from 1 to 9"); //tell users the formula and condition.
double a, b, c, d, e, f, x, y;
int k;
stop:{
while (true) {
try{ // use try...catch to make sure that users input numbers not letters or symbols.
k =0;
do {
if (k != 3) { //Users can only try 3 times or it will do the else part below.
System.out.print("a = ");
a = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((a<1 && a>-1 )|| a>9 || a < -9); // If users input invalid value, the do part will repeat.
k = 0;
do {
if (k != 3) {
System.out.print("b = ");
b = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((b<1 && b>-1 )|| b>9 || b < -9);
System.out.print("e = ");
e = s.nextDouble();
}catch (Exception v){
System.out.println("Please ONLY input numbers. Exiting...");
break; //To end the program.
}
if (a - Math.floor(a) != 0){ //Math.floor(a) is to get the integral part of a.
System.out.print(a + "x ");
}else {
System.out.printf("%.0fx ", a);
}
if (b > 0){
if (b - Math.floor(b) != 0){
System.out.print("+ " + b +"y = ");
}else {
System.out.printf("+ %.0fy = ", b);
}
}else{
if (b - Math.floor(b) != 0){
System.out.print("- " + Math.abs(b) +"y = ");
}else {
System.out.printf("- %.0fy = ", Math.abs(b));
}
}
if (e - Math.floor(e) != 0){
System.out.print(e + "\n");
}else {
System.out.printf("%.0f\n", e);
}
try{
k = 0;
do {
if (k != 3) {
System.out.print("c = ");
c = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((c<1 && c>-1 )|| c>9 || c < -9);
k = 0;
do {
if (k != 3) {
System.out.print("d = ");
d = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((d<1 && d>-1 )|| d>9 || d < -9);
System.out.print("f = ");
f = s.nextDouble();
}catch (Exception v){
System.out.println("Please ONLY input numbers. Exiting...");
break;
}
if (c - Math.floor(c) != 0){
System.out.print(c + "x ");
}else {
System.out.printf("%.0fx ", c);
}
if (d > 0){
if (d - Math.floor(d) != 0){
System.out.print("+ " + d +"y = ");
}else {
System.out.printf("+ %.0fy = ", d);
}
}else{
if (d - Math.floor(d) != 0){
System.out.print("- " + Math.abs(d) +"y = ");
}else {
System.out.printf("- %.0fy = ", Math.abs(d));
}
}
if (f - Math.floor(f) != 0){
System.out.print(f + "\n");
}else {
System.out.printf("%.0f\n", f);
}
System.out.println();
if (a * d - b * c != 0) {
x = (e * d - b * f) / (a * d - b * c);
y = (a * f - e * c) / (a * d - b * c);
System.out.println("The results are : ");
if (x - Math.floor(x) != 0) {
System.out.println("x = " + x);
}else{
System.out.printf("x = %.0f\n", x);
}
if (y - Math.floor(y) != 0) {
System.out.println("y = " + y);
}else{
System.out.printf("y = %.0f\n", y);
}
break stop;
} else {
System.out.println("The equation has no solution");
break stop;
}
}
}
}
}
* @author: Niu Zhaohang
*
* This includes :
* a) Checker for types of values users input by using [try...catch].
* (They can only input numbers or the program will die.)
* b) Counter for times that users input out-ranged numbers by using [do...while].
* (They can only try 3 times or the program will die.)
* c) Checker for types of numbers users input by using [if...else, printf]
* (If they input integers, the program will form the formula by using integers instead of double.)
* d) Calculator for x, y.
**/
import java.util.Scanner;
public class CramersRule {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("ax + by = e , cx + dy = f, ATTENTION: a,b,c,d is from 1 to 9"); //tell users the formula and condition.
double a, b, c, d, e, f, x, y;
int k;
stop:{
while (true) {
try{ // use try...catch to make sure that users input numbers not letters or symbols.
k =0;
do {
if (k != 3) { //Users can only try 3 times or it will do the else part below.
System.out.print("a = ");
a = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((a<1 && a>-1 )|| a>9 || a < -9); // If users input invalid value, the do part will repeat.
k = 0;
do {
if (k != 3) {
System.out.print("b = ");
b = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((b<1 && b>-1 )|| b>9 || b < -9);
System.out.print("e = ");
e = s.nextDouble();
}catch (Exception v){
System.out.println("Please ONLY input numbers. Exiting...");
break; //To end the program.
}
if (a - Math.floor(a) != 0){ //Math.floor(a) is to get the integral part of a.
System.out.print(a + "x ");
}else {
System.out.printf("%.0fx ", a);
}
if (b > 0){
if (b - Math.floor(b) != 0){
System.out.print("+ " + b +"y = ");
}else {
System.out.printf("+ %.0fy = ", b);
}
}else{
if (b - Math.floor(b) != 0){
System.out.print("- " + Math.abs(b) +"y = ");
}else {
System.out.printf("- %.0fy = ", Math.abs(b));
}
}
if (e - Math.floor(e) != 0){
System.out.print(e + "\n");
}else {
System.out.printf("%.0f\n", e);
}
try{
k = 0;
do {
if (k != 3) {
System.out.print("c = ");
c = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((c<1 && c>-1 )|| c>9 || c < -9);
k = 0;
do {
if (k != 3) {
System.out.print("d = ");
d = s.nextDouble();
k++;
}else{
System.out.println("Invalid Values, Exiting...");
break stop;
}
} while ((d<1 && d>-1 )|| d>9 || d < -9);
System.out.print("f = ");
f = s.nextDouble();
}catch (Exception v){
System.out.println("Please ONLY input numbers. Exiting...");
break;
}
if (c - Math.floor(c) != 0){
System.out.print(c + "x ");
}else {
System.out.printf("%.0fx ", c);
}
if (d > 0){
if (d - Math.floor(d) != 0){
System.out.print("+ " + d +"y = ");
}else {
System.out.printf("+ %.0fy = ", d);
}
}else{
if (d - Math.floor(d) != 0){
System.out.print("- " + Math.abs(d) +"y = ");
}else {
System.out.printf("- %.0fy = ", Math.abs(d));
}
}
if (f - Math.floor(f) != 0){
System.out.print(f + "\n");
}else {
System.out.printf("%.0f\n", f);
}
System.out.println();
if (a * d - b * c != 0) {
x = (e * d - b * f) / (a * d - b * c);
y = (a * f - e * c) / (a * d - b * c);
System.out.println("The results are : ");
if (x - Math.floor(x) != 0) {
System.out.println("x = " + x);
}else{
System.out.printf("x = %.0f\n", x);
}
if (y - Math.floor(y) != 0) {
System.out.println("y = " + y);
}else{
System.out.printf("y = %.0f\n", y);
}
break stop;
} else {
System.out.println("The equation has no solution");
break stop;
}
}
}
}
}