Advertisement
ambiorixdr

Multiply an Array of Doubles

Mar 10th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class MultiplyanArrayofDoubles {
  5.     public static void main(String[] args) {
  6.         Scanner console = new Scanner(System.in);
  7.  
  8.         String[] numbers = console.nextLine().split(" ");
  9.         double p = Double.parseDouble(console.nextLine()); // It was int but it should be double
  10.  
  11.         double[] numersToMultiply = new double[numbers.length];
  12.  
  13.         for (int i = 0; i < numbers.length; i++) {
  14.             numersToMultiply[i] = Double.parseDouble(numbers[i]);
  15.         }
  16.  
  17.         for (int i = 0; i < numersToMultiply.length; i++) {
  18.             numersToMultiply[i] *= p;
  19.         }
  20.  
  21.         DecimalFormat df = new DecimalFormat("##.##");
  22.  
  23.         for (int i = 0; i < numersToMultiply.length; i++) {
  24.             System.out.print(df.format(numersToMultiply[i]) + " ");
  25.         }
  26.  
  27.         // Previous solution:
  28.         // String result = String.join(" ", Arrays.toString(numersToMultiply));
  29.         // System.out.println(result);
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement