简体   繁体   中英

Too many decimal points in Java program

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 (ie -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.

You are using doubles, which are notoriously not exact. Since not all values can be represented with doubles, sometimes values are approximated, as you see here. If you want more accurate results, try using a fixed point representation instead of a floating point one.

What is wrong with those two coordinates?

Nothing really. That's unfortunately how floating point numbers work .

I've looked into formatting, but I don't know how to implement it into my code while also printing the text

You could say

System.out.format("Bottom Right: (%2.3f,%2.3f)%n", xbottomright, ybottomright);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM