得到“NaN”的方程组计算器

时间:2015-04-29 04:59:45

标签: java math nan

这是我的代码。

 import java.util.Scanner;
 import java.util.ArrayList;
  public class SysQ {

public static void main(String[] args) {
    Scanner keyboard = new Scanner (System.in);


System.out.print("Enter A: ");
double a = keyboard.nextDouble();
System.out.print("Enter B: ");
double b = keyboard.nextDouble();
System.out.print("Enter C: ");
double c = keyboard.nextDouble();
System.out.print("Enter X: ");
double x = keyboard.nextDouble();
System.out.print("Enter Y: ");
double y = keyboard.nextDouble();
System.out.print("Enter Z: ");
double z = keyboard.nextDouble();


   double fixedequation2 = x * b;
   double fixedequation3 = x * c;
   double fixedequation5 = a * y;
   double fixedequation6 = a * z;



double final1 = fixedequation2 - fixedequation5;
double final2 = fixedequation3 - fixedequation6;
double yanswer = final2/final1;



double x1 = c - (b * yanswer);
double xanswer = x1/a;

System.out.println("X is " + xanswer);
System.out.println("Y is " + yanswer);

因此代码适用于2 x 2方程的大多数数字,但是当我为a,b,c,x,y,z输入值5,5,5,5,5,5时,我得到“NaN”我只是想知道“NaN”是指无限多的解决方案还是没有解决方案......

1 个答案:

答案 0 :(得分:2)

NaN 表示“不是数字”。你得到了那个解决方案,因为你正在做

(25-25)/(25-25) = 0/0,结果未定义,或“不是数字”