I came across a math problem the other day and decided to write some Java code to figure out the answer. Except my code does not work for some odd reason.
Here is the problem:
Here is my code:
public class Java {
public static void main(String[] args) {
double tLeft = 0;
double tRight = 0;
double bLeft = 0;
double bRight = 0;
while (bLeft - bRight != 6) {
tLeft += 0.1;
tRight = 8 - tLeft;
bLeft = 13 - tLeft;
bRight = 8 - tRight;
}
System.out.printf("Top Left = %f\nTop Right = %f\nBottom Left = %f\nBottom Right = %f\n\n", tLeft, tRight, bLeft, bRight);
}
}
This will loop through all possible variations of the Top Left number and give me numbers based off of that. Yes I know it is very clumsy and bad code, but I am still learning!
The problem here though is that my IDE (Eclipse) will just loop infinitely and will not exit out of the loop and print the answer. If I change tLeft to be declared as equal to 3 instead of 0, I get my answer, but if I go even further and declare it as 2.7 instead of 3, Eclipse decides to loop infinitely again.
My question to you all is exactly why and when does Eclipse decide to infinitely loop and how can I prevent it in order to get the answer I want?
P.S. the answers to the math problem are:
Top Left = 3.5
Top Right = 4.5
Bottom Left = 9.5
Bottom Right = 3.5
Thank you in advance!