StefanTobler

Lesson 10 Activity

Sep 19th, 2016
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. /*
  2.  * Lesson 10 Coding Activity Question 1
  3.  *
  4.  * The following calculation will give roundoff error:
  5.  *      double x = 1.473;
  6.  *      System.out.println(2 - x);
  7.  *
  8.  * Write the code to correct it. You can assume no more than 3 decimal places will be used.
  9.  *
  10.  * Sample run:
  11.  *
  12.  *      Please enter two decimal values:
  13.  *      2
  14.  *      1.473
  15.  *    
  16.  *      The difference is: 0.527
  17.  *
  18.  * Use this starter file as your template.
  19.  *
  20. */
  21.  
  22. import java.util.Scanner;
  23. import java.lang.Math;
  24.  
  25.  
  26. class Lesson_10_Activity {
  27.     public static void main(String[] args) {
  28.       Scanner scan = new Scanner (System.in);
  29.       System.out.println("Please enter two decimal values:");
  30.       double x = scan.nextDouble();
  31.       double y = scan.nextDouble();
  32.       System.out.println("The difference is: " + ((double)((Math.round(x * 1000)) - (Math.round(y * 1000)))/1000));
  33.  
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment