Question

In: Computer Science

How do i remove the decimals out of the output? Do I need to int a...

How do i remove the decimals out of the output? Do I need to int a new value, or use Math.round?

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double numbers[] = new double[5];
  
inputArray(numbers);
maxNumber(numbers);
minNumber(numbers);
}
  
public static void inputArray( double[] numbers) {
Scanner in = new Scanner(System.in);
  
for(int i = 0; i < numbers.length; i++) {
numbers[i] = in.nextDouble();
}
}
  
public static void maxNumber(double[] numbers) {
double maxNum = numbers[0];
  
for(int i = 0; i < numbers.length; i++) {
if(numbers[i] > maxNum ) {
maxNum = numbers[i];
}
}
  
System.out.println("Max: "+maxNum);
}
  
public static void minNumber(double[] numbers) {
double minNum = numbers[0];

for(int i = 0; i < numbers.length; i++) {
if(numbers[i] < minNum ) {
minNum = numbers[i];
}
}
  
System.out.println("Min: "+minNum);
}
}

Solutions

Expert Solution

Answer:

As you asked that you just wanted to remove the decimal places for that you need to create new int value and type cast the output value with decimal places into the integer by doing that it will remove the values after the decimal point.

But using Math.round() is not encouraged because it will convert the value to nearly round value like if the values in 99.99 it will gives you the output 100 but we only need 99 and .99 should be removed to get that we need to create a new int value

Here is the modification of you code to meet your requirement:

Raw code:

import java.util.Scanner;

import java.lang.Math;

public class Main {

public static void main(String[] args) {

double numbers[] = new double[5];

inputArray(numbers);

maxNumber(numbers);

minNumber(numbers);

}

public static void inputArray( double[] numbers) {

Scanner in = new Scanner(System.in);

for(int i = 0; i < numbers.length; i++) {

numbers[i] = in.nextDouble();

}

}

public static void maxNumber(double[] numbers) {

double maxNum = numbers[0];

for(int i = 0; i < numbers.length; i++) {

if(numbers[i] > maxNum ) {

maxNum = numbers[i];

}

}

//creating new int value with type casting

int maxValue= (int)maxNum;

System.out.println("Max: "+maxValue);

}

public static void minNumber(double[] numbers) {

double minNum = numbers[0];

for(int i = 0; i < numbers.length; i++) {

if(numbers[i] < minNum ) {

minNum = numbers[i];

}

}

//creating new int value with type casting

int minValue=(int)minNum;

System.out.println("Min: "+minValue);

}

}

Code in the editor:

output:

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.


Related Solutions

C# using MindTap How do I round this up to 2 decimals: I need the total...
C# using MindTap How do I round this up to 2 decimals: I need the total to round to two decimals example instead of the output to be 119.625 it needs to be 119.63 using System; using static System.Console; using System.Globalization; class DemoJobs { static void Main() { Job job1 = new Job(); Job job2 = new Job(); Job job3 = new Job(); job1.Description = "wash windows"; job1.Time = 3.5; job1.Hourly = 25.00; job2.Description = "clean garden"; job2.Time = 2;...
How do I convert the integers in my 2D array list to output decimals numbers? My...
How do I convert the integers in my 2D array list to output decimals numbers? My program works accepting regular integers, but crashes when decimals are entered into my 2D array list. Can someone improve it so it accepts decimal numbers? Here is my code: import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TwoDimArray { private int numbers[][]; public TwoDimArray() { loadArray(); } public void loadArray() { /* //loadArray() method loads the users defined filename //@return returns the...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
I need to write java program that do replace first, last, and remove nth character that...
I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods. No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive. if user's input was "be be be" and replace first 'b' replace first should do "e be be" replace last should do "be be e" remove nth character such as if user want to remove 2nd b it...
Just Need to see how the dataset and the output will Look in SPSS for I...
Just Need to see how the dataset and the output will Look in SPSS for I can check my answer. Please use SPSS... I need the answer ASAP.. Thanks. Only SPSS Chi-Square Test of Independence satisfied Freshmen Sophomore Junior Senior Yes 21 19 10 20 No 14 9 17 11
How do I start to code in mysql to Alter table to modify column remove not...
How do I start to code in mysql to Alter table to modify column remove not null constraint First drop the foreign key associated with Drop the column Add the column back with new definition Add the foreign key back This Is what I wrote so far alter table employees modify column repotsTo remove null:
-please answer this written out. Do not use excel I cannot access it. i need to...
-please answer this written out. Do not use excel I cannot access it. i need to know how to do it on paper- Deluxe River Cruises operates a fleet of river vessels. The fleet has two types of vessels: A type A vessel has 60 deluxe cabins and 160 standard cabins, whereas a type B vessel has 80 deluxe cabins and 120 standard cabins. Under a charter agreement with the Odyssey Travel Agency, Deluxe River Cruise sis to provide Odyssey...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT