I have to write a code that tells the user to input 4 number (they could be decimal) and print the average and lowest of those numbers.
So far I have managed to get the average, but I'm having trouble getting the lowest of the numbers.
int iteration = 0;
float number;
float total = 0;
float average;
float lowest;
Scanner input = new Scanner(System.in);
while (iteration < 4){
System.out.println("Enter score : ");
number = input.nextFloat();
iteration++;
total += number;
}
average = total / 4;
System.out.println("The average is: " + average);
You can initialize your lowest with value Float.MAX_VALUE, everytime the user inputs a value, you compare your lowest with the input value and assign the new smaller value to your lowest.
int iteration = 0;
float number;
float total = 0;
float average;
float lowest = Float.MAX_VALUE;
Scanner input = new Scanner(System.in);
while (iteration < 4){
System.out.println("Enter score : ");
number = input.nextFloat();
iteration++;
total += number;
if(number < lowest){
lowest = number;
}
}
average = total / 4;
System.out.println("The average is: " + average);
System.out.println("The minimum is: " + lowest);
Try this:
float minValue = Double.MAX_VALUE;
while(...){
...
number = input.nextFloat();
minValue = Math.min(minValue, number);
...
}
Hope this will help you:).
Initially define float lowest = some big no.
Now Every time you enter an input compare it with the lowest. If the input is less than lowest assign it as lowest, otherwise don't change lowest.
As said by Olli Zi you can get the min value by using just a simple function Math.min().Moreover for detailed understanding i am providing the code as well.
int iteration = 0;
float number[]=new float[4];
float total = 0;
float average;
float lowest;
Scanner input = new Scanner(System.in);
while (iteration < 4){
System.out.println("Enter score : ");
number[iteration] = input.nextFloat();
total += number[iteration];
iteration=iteration+1;
}
average = total / 4;
System.out.println("The average is: " + average);
System.out.println(Math.min(Math.min(number[0],number[1]), Math.min(number[2],number[3])));
Hope it helps!
Related
package HW2_Min_Max;
import java.util.Scanner;
public class HW2_Min_Max {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Please input a positive interger that indicates number of positive intergers ");
int number = myScanner.nextInt();
while (number <= 0) {
System.out.println("Please input interger");
number = myScanner.nextInt();
}
int i=1; //i is to store current iteration
int sum=0; //sum is to store sum of the input
int x; //x is to store the user input
while (i <= number){
System.out.println("Please input a positive interger ");
x = myScanner.nextInt();
sum = sum + x;
i++;
}
int average = sum/number;
System.out.println("The average is " + average);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
if (number < min){
min = number;
}
if (number > max) {
max = number;
}
System.out.println("The minimum value is " + min);
System.out.print( "and the maximum value is" + max);
}
}
}
1.^ this is where i am getting my problem, on the very last brace in Netbeans i am getting an error that says "class, interface, or enum expected" but i have no idea why. Excuse my ignorance as I am a very fresh beginner with java, let alone programming.
For the first part, I would suggest you use a do-while loop to test for the number of numbers. Like,
int number;
do {
System.out.println("Please input a positive integer that "
+ "indicates number of positive integers ");
number = myScanner.nextInt();
} while (number <= 0);
Then you need to set min and max in your loop (or store all the values you read). I would prefer Math.max and Math.min. I would also count from 0. Like,
int i = 0; // i is to store current iteration
int sum = 0; // sum is to store sum of the input
int x; // x is to store the user input
int max = Integer.MIN_VALUE; // store the max user input
int min = Integer.MAX_VALUE; // store the min user input
while (i < number) {
System.out.println("Please input a positive integer ");
x = myScanner.nextInt();
if (x > 0) { // make sure it's a positive integer
min = Math.min(min, x);
max = Math.max(max, x);
sum += x;
i++;
}
}
int average = sum / number;
System.out.println("The average is " + average);
System.out.println("The minimum value is " + min);
System.out.println("and the maximum value is " + max);
Its working fine now. I have made some changes in it. Kindly consider it. I hope it would help.
Scanner myScanner = new Scanner(System.in);
System.out.println("Please input a positive interger that indicates number of positive intergers ");
int number = myScanner.nextInt();
while (number <= 0) {
System.out.println("Please input interger");
number = myScanner.nextInt();
}
int[] arr = new int[number]; // Store values in array
int i=0; //i is to store current iteration
int sum=0; //sum is to store sum of the input
int x; //x is to store the user input
int max = 0;
int min = 0;
for(i=0;i<number;i++){
System.out.println("Please input a positive interger ");
arr[i] = myScanner.nextInt();
sum = sum + arr[i];
}
int average = sum/number;
System.out.println("The average is " + average);
// Put initial values in min and max for comparing
min =arr[0];
max = arr[0];
for(i=1;i<number;i++)
{
// Compare if values is less than arr[i]
if (arr[i] < min){
min = arr[i];
}
// Compare if values is greater than arr[i]
if (arr[i] > max) {
max = arr[i];
}
}
System.out.print("The minimum value is " + min);
System.out.println( " and the maximum value is " + max);
}
}
Here is the output:
Not sure where to begin honestly, I was able to find average using the user's input but can't seem to figure out the largest number or the smallest number from the numbers entered. Is there a method I can use? Any help would be great. Thank you.
EDIT: Somewhat figured it out. However the answers always seem to be largest number = 2.1478... or Smallest number = -2.1478...
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;
double total=0;
int count = 1;
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double somenumber = input.nextDouble();
total+=somenumber;
count++;
if (somenumber > max){
max = somenumber;
}
if (somenumber < min){
min = somenumber;
}
}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Largest number is " + max);
System.out.println("Smallest number is" + min);
}
}
You want some code that looks like
double min = Double.MAX_VALUE;
double max = 0.00; // assume use positive numbers
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double somenumber = input.nextDouble();
total+=somenumber;
count++;
max = Math.max (somenumber, max);
min = Math.min (somenumber, min);
}
EDIT Updated to use Double instead and now uses your original loop condition.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double smallest = Double.POSITIVE_INFINITY; //Initialize to largest int possible
double largest = Double.NEGATIVE_INFINITY; //Initialize to smallest int possible
System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
int count = 1;
while (count < random + 1) {
System.out.print("Input Number: ");
Double x = input.nextDouble();
if (x < smallest) {
smallest = x;
}
if (x > largest) {
largest = x;
}
count++;
}
System.out.println("Smallest: " + smallest + ", Largest: " + largest);
}
}
Sample run:
run:
Enter the number of grades:
5
Input Number: 2.5454
Input Number: 3.454
Input Number: -100.54
Input Number: 5687
Input Number: 579873.5654
Smallest: -100.54, Largest: 579873.5654
BUILD SUCCESSFUL (total time: 15 seconds)
Thank you for the tips everyone. However, when I ran my program. I wasn't able to get the proper answers for the max and min value.
EDIT: SOLVED. THANK YOU ALL WHO CONTRIBUTED. <33
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;
double total=0;
int count = 1;
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double somenumber = input.nextDouble();
total+=somenumber;
count++;
if (somenumber > max){
max = somenumber;
}
if (somenumber < min){
min = somenumber;
}
}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Largest number is " + max);
System.out.println("Smallest number is" + min);
}
}
Largest number is 2.14748 Smallest number is -2.147478...That's what was printed...Am I making a arthimetic error?
Above while loop...
double max = 0;
double min = Double.MIN_VALUE;
In loop after you set some number...
if (someNumber > max){
max = someNumber;
}
if (someNumber < min){
min = someNumber;
}
Np glad it helped :) you should consider using a for loop instead of a while loop and declare someNumber outside of the loop like so:
double someNumber;
for (int i = 1; i <= random; i++){
//...
}
To make it even cleaner you could replace i <= random in the for loop with
i <= readNumGrades(input);
private double readNumGrades(Scanner in){
System.out.println("Enter the number of grades");
return in.nextDouble();
}
So you no longer need the variables count and random
public static void main(String[] args) {
int sum = 0;
int inputNum;
int counter;
float average;
double Max = 0;
double Min = 101;
Scanner NumScanner = new Scanner(System.in);
Scanner charScanner = new Scanner(System.in);
System.out.println("Enter the total number of exams you want a average");
counter = NumScanner.nextInt();
System.out.println("Please enter " + counter + " numbers:");
for(int i = 1; i<=counter ;i++){
inputNum = NumScanner.nextInt();
sum = sum + inputNum;
System.out.println();
if(inputNum > Max){
Max = inputNum;
}
if(inputNum < Min){
Min = inputNum;
}
if(inputNum > -1 && inputNum < 101){
sum = sum + inputNum;
}
else{
System.out.println("You entered a number that wasn't in the range of 0 to 100");
average = sum / counter;
}
}
}
}
Write a program using a loop that takes 10 values from a user representing exam grades (between 0 and 100) from the keyboard and outputs the minimum value, maximum value and average value of all the values entered. Your program should not accept values less than 0 or greater than 100. Problems with calculation of average and the program does not print out max and min values
How can I do this?
//This code is how to print out the max and min values of a list of numbers from the above program//
// Print out of max and min exam grades//
System.out.println( "Max Exam Score = " + Max );
System.out.println( "Minimum Exam Score = " + Min `enter code here`);
does anyone knows how to calculate the average in a loop. Every time I calculated the average I received 0 or 1. I know that I need use average = (sum) / (salary_annually); but I can't get it to work. Thanks in advance.
import java.util.Scanner;
public class midterm
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
int average=0;
int count = 0;
int salary_annually = 0;
for(int employee =1; employee <= 2; employee++)
{
System.out.println("Employee: " + employee);
for(int year=1; year <= 2; year++)
{
System.out.println("Please Enter the Salary for Year: " + year);
salary_annually = kb.nextInt();
sum += salary_annually ;
if (min >= salary_annually)
{
min = salary_annually;
}
if (max <=salary_annually)
{
max = salary_annually;
}
average = (sum) / (salary_annually);
}
System.out.println("The average is " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}
}
I'm guessing the problem here is that you are using integer division. Since the sum and salary_annually are both integers division works slightly different. There is not remainder because dividing two integers gives an int.
For example 1/2 is not .5 as you might expect but instead it is 0. Any fractional number is dropped when doing integer math. As another example 9/5 is not 1.8 but instead 1.
If you want the average then you can either declare sum or salary_annually as a double and declare the average as a double as well.
Change
average = (sum) / (salary_annually);
To
double average=0;// Declare it as `double` rather than `int`
average = (sum) / 2.0;
average is calculated by: average = sum / count;
you need to increment your count variable, otherwise you will always get and ArithmeticException / by zero
The Average is calculated by average = sum / count where average should be of type double.
You did declare the variable count, but didn't use it.
import java.util.Scanner;
public class Calulate {
/**
* #param args
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
double average = 0;
int count = 2;
int salary_annually = 0;
for (int employee = 1; employee <= 2; employee++) {
System.out.println("Employee: " + employee);
for (int year = 1; year <= count; year++) {
System.out.println("Please Enter the Salary for Year: " + year);
salary_annually = kb.nextInt();
sum += salary_annually;
if (min >= salary_annually) {
min = salary_annually;
}
if (max <= salary_annually) {
max = salary_annually;
}
}
average = sum / count;
System.out.println("The average is " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}
}
I just found that when I calculate the average for the second user, I get a different result. For example, if I add this number for the first user 10,10,10,20,20. I get 14.0 which is correct. But when I enter the same number for the second user I get 28.0? any ideas?
import java.util.Scanner;
public class midterm
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
double average=0;
int count = 0;
int salary_annually = 0;
for(int employee =1; employee <= 2; employee++)
{
System.out.println("Employee: " + employee);
for(int year=1; year <= 5; year++)
{
System.out.println("Please Enter the Salary for Year: " + year);
salary_annually = kb.nextInt();
sum += salary_annually ;
average = (sum) / 5.0;
if (min >= salary_annually)
{
min = salary_annually;
}
if (max <=salary_annually)
{
max = salary_annually;
}
}
System.out.println("The average is " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}
}
sum = 0; // this before 2nd loop
int max = Integer.MIN_VALUE; // these too.
int min = Integer.MAX_VALUE;
for(int year=1; year <= 5; year++)
{
.
.
.
}
average = (sum) / 5.0; // this after 2nd loop
In your solution you are calculating average 5 times inside the loop! and you only get advantage from the fifth time! must be after the loop. average = (sum) / 5.0; after the loop.
You need to zero sum at the end of the loop. As it is, now it just keeps increasing and increasing as the old value is kept in memory, when coming to the next iteration.