So this is one of the practice questions for my computer science exam tomorrow. For some reason, whatever numbers I enter, the result comes up with this:
Why is the answer always NaN? Someone tell me what I'm doing wrong? Here is the code:Enter y2:
5
Enter y1:
2
Enter x2:
4
Enter x1:
2
The solution to the equation (y2-y1) / (x2 - x1) is NaN.
import java.io.*; class questionone { public static void main (String [] args) throws IOException { InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader stdin = new BufferedReader (inStream); double y2; double y1; double x2; double x1; System.out.println ("Enter y2: "); y2 = Double.parseDouble (stdin.readLine ()); System.out.println ("Enter y1: "); y1 = Double.parseDouble (stdin.readLine ()); System.out.println ("Enter x2: "); x2 = Double.parseDouble (stdin.readLine ()); System.out.println ("Enter x1: "); x1 = Double.parseDouble (stdin.readLine ()); Calculations calculate = new Calculations (x1, x2, y1, y2); System.out.println (calculate.receive()); } } class Calculations { double x1; double x2; double y1; double y2; Calculations (double x1, double x2, double y1, double y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } double numerator = y2 - y1; double denominator = x2 - x1; double solution = numerator / denominator; String receive () { return ("The solution to the equation (y2-y1) / (x2 - x1) is " + solution + "."); } }
Thanks