I recently began my first semester of Java Programming, and reached an impasse on an assignment. The assignment is to request user input for points on a rectangle. The user must enter the height, width, bottom left x coordinate, and bottom left y coordinate. I finally managed to write the program, and it compiled with no errors. My problem is that when I use numbers with decimals (i.e. -4.3, 8.7, etc), the top left x and top right x coordinates have way too many decimal points.
Why are these the only points showing too many decimals? I only need one decimal point (5.6, 3.4, etc).
I will show you my code and the output. If my code is sloppy, I apologize. I'm very new to this:
import java.util.Scanner;
public class rectanglePoints {
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter bottom/left x coordinate: ");
double xbottomleft = input.nextDouble();
System.out.print("Please enter bottom/left y coordinate: ");
double ybottomleft = input.nextDouble();
System.out.print("Please enter width: ");
double width = input.nextDouble();
System.out.print("Please enter height: ");
double height = input.nextDouble();
System.out.println("Bottom Left: (" + xbottomleft + "," + ybottomleft + ")");
double xbottomright = xbottomleft;
double ybottomright = ybottomleft + width;
double xtopleft = xbottomleft + height;
double ytopleft = ybottomleft;
double xtopright = xtopleft;
double ytopright = ytopleft + width;
System.out.println("Bottom Right: (" + xbottomright + "," + ybottomright + ")");
System.out.println("Top Left: (" + xtopleft + "," + ytopleft + ")");
System.out.println("Top Right: (" + xtopright + "," + ytopright + ")");
}
}
Here is what the output shows when using randomly picked numbers for the input:
Please enter bottom/left x coordinate: -4.3 Please enter bottom/left y coordinate: 3.5 Please enter width: 7.6 Please enter height: 8.7 Bottom Left: (-4.3,3.5) Bottom Right: (-4.3,11.1) Top Left: (4.3999999999999995,3.5) Top Right: (4.3999999999999995,11.1)
What is wrong with those two coordinates? I've looked into formatting, but I don't know how to implement it into my code while also printing the text, such as "Bottom Right:", "Top Left:", etc.