I was given the rainfall question where total rainfall is to be calculated for three months and then the average is to be determined however for some reason the loop will not end at three entries. I've defined the counter and have even used 'break' but for some reason the compiler just allows the user to keep entering. If the loop does not end I cannot calculate the average rainfall
I've defined the loop and created a while statement
import java.util.Scanner;
public class rainfall {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double average;
double total = 0;
final double rainfall[] = new double[3];
for (int counter = 0; counter <= rainfall.length; counter++) {
do {
System.out.print("Enter amount of rainfall:");
rainfall[counter] = input.nextInt();
} // end of do statement
while (rainfall[counter] <= 3);
} // end of for statement
for (int counter = 0; counter <= rainfall.length; counter++) {
System.out.println(counter + "t\t" + rainfall[counter]);
// display rainfall in the array
total = total + rainfall[counter]; //calculate total rainfall
System.out.println("Total rainfall is:" + total);
average = total / 3; //calculate average rainfall
System.out.print("Average rainfall is" + average);
}
}
}
I expect that the user could only enter results for three months and then the average will be given afterwards however the loop will not stop and hence the average can't be worked out.
That's the version which works:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double average;
double total = 0;
final double rainfall[] = new double[3];
for (int counter = 0; counter < rainfall.length; counter++) {
System.out.print("Enter amount of rainfall:");
rainfall[counter] = input.nextInt();
} // end of for statement
for (int counter = 0; counter < rainfall.length; counter++) {
System.out.println(counter + "t\t" + rainfall[counter]);
// display rainfall in the array
total = total + rainfall[counter]; //calculate total rainfall
System.out.println("Total rainfall is:" + total);
average = total / 3; //calculate average rainfall
System.out.print("Average rainfall is" + average);
}
}
The while loop was unnecessary and your for-loops with <= were actually designed for four elements instead of three.
You have a while loop that won't increment your for loop unless the input is greater than 3. You want to do something like this:
for (int counter = 0; counter < rianfall.length; counter++) {
System.out.print("Enter amount of rainfall:");
rainfall[counter] = input.nextInt();
total += rainfall[counter];
}
System.out.println("Total rainfall is:" + total);
average = total / rainfall.length; //calculate average rainfall
System.out.print("Average rainfall is" + average);
I condensed your two for loops into one and put the printing of the total and average outside the loop to only do it once.
Related
Hi Im new to java and trying to do some exercises to familiarize myself with it. I am trying to calculate the sum and average of the user input numbers using a for each or enhanced for loop. What i have done is to store the numbers in an array. Can you please check if my code is correct.
Scanner scanner = new Scanner(System.in);
double inputNum;
double sum= 0;
int counter;
System.out.println("How many entries would you like input");
int entry = scanner.nextInt();
for (counter = 0; counter< entry; counter++){
double [] numbers = new double[entry];
System.out.println("Please input number " + (counter +1));
numbers[counter] = scanner.nextDouble();
for (double x : numbers[counter]) {
x += sum;
}
System.out.println("The total sum is: " + sum);
double average = sum/entry;
System.out.println("The total average is: " + average);
}
You don't need the inner loop. You can add each input to the sum after you store it in the array.
In addition, you should allocate the array once outside the outer loop, and not in each iteration.
double [] numbers = new double[entry];
for (counter = 0; counter< entry; counter++){
System.out.println("Please input number " + (counter +1));
numbers[counter] = scanner.nextDouble();
sum+= numbers[counter];
}
System.out.println("The total sum is: " + sum);
double average = sum/entry;
System.out.println("The total average is: " + average);
Put the sum and average logic outside the loop. This should work:
double [] numbers = new double[entry];
for (counter = 0; counter< entry; counter++){
System.out.println("Please input number " + (counter +1));
numbers[counter] = scanner.nextDouble();
sum += numbers[counter];
}
System.out.println("The total sum is: " + sum);
double average = sum/entry;
System.out.println("The total average is: " + average);
You can't use a for-each with a scanner. Also there is no need your you to be storing anything. Just keep track of the sum.
Scanner scanner = new Scanner(System.in);
System.out.println("How many entries would you like input?");
int numEntries = scanner.nextInt();
double sum = 0;
for (counter = 1; counter <= numEntries; counter++) {
System.out.println("Please input number " + counter + ": ");
sum += scanner.nextDouble();
}
System.out.println("The total sum is: " + sum);
System.out.println("The average is: " + sum / numEntries);
Same code, with average outside the loop and the useless array removed:
Scanner scanner = new Scanner(System.in);
double sum = 0;
System.out.println("How many entries would you like input");
int entry = scanner.nextInt();
for (int counter = 1; counter <= entry; counter++) {
System.out.println("Please input number " + counter);
sum += scanner.nextDouble();
}
System.out.println("The total sum is: " + sum);
double average = sum / entry;
System.out.println("The total average is: " + average);
Everything is working fine, I just need my output to say the quarter with the highest/lowest rainfall, not the actual values. I am not sure how to tie the quarter and the values together so that the output will be quarter 1, 2, 3 or 4.
import java.util.Scanner;
public class rainfall
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double[] rainfall = new double[4];
double totalRainfall = 0.0;
double max = 0, min = 0;
for (int i=0; i < 4; i++)
{
System.out.print("Enter rainfall for quarter " + (i+1) + ": ");
rainfall[i] = scan.nextDouble();
totalRainfall += rainfall[i];
if (i == 0)
{
max = min = rainfall[i];
}
{
if (rainfall[i] > max)
max = rainfall[i];
else if (rainfall[i] < min)
min = (i + 1);
//min = rainfall[i];
}
}
System.out.println("Total rainfall = "+totalRainfall);
System.out.println("Average rainfall = "+(totalRainfall / 4.0));
System.out.println("Max quarter rainfall = "+ max);
System.out.println("Min quarter rainfall = " + min);
//System.out.println("Max quarter rainfall = "+ maxQuarter);
//System.out.println("Min quarter rainfall = " + minQuarter);
}//end main
}//end class
Just store the indexes instead of the values:
if (rainfall[i] > rainfall[max])
max = i;
else if (rainfall[i] < rainfall[min])
min = i;
System.out.println("Max quarter rainfall = " + max + 1);
System.out.println("Min quarter rainfall = " + min + 1);
You need to track maxQuarter and minQuarter (added as ints) as well as max and min. Like so:
import java.util.Scanner;
public class rainfall
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double[] rainfall = new double[4];
double totalRainfall = 0.0;
double max = 0, min = 0;
// init to Q1 as that's the 1st tested
int maxQuarter = 1, minQuarter = 1;
for (int i=0; i < 4; i++)
{
System.out.print("Enter rainfall for quarter " + (i+1) + ": ");
rainfall[i] = scan.nextDouble();
totalRainfall += rainfall[i];
if (i == 0)
{
max = min = rainfall[i];
}
{
if (rainfall[i] > max) {
max = rainfall[i];
maxQuarter = i + 1;
}
if (rainfall[i] < min) {
min = rainfall[i];
minQuarter = i + 1;
}
}
}
System.out.println("Total rainfall = "+totalRainfall);
System.out.println("Average rainfall = "+(totalRainfall / 4.0));
System.out.println("Max quarter rainfall = "+ max);
System.out.println("Min quarter rainfall = " + min);
System.out.println("Max quarter rainfall = "+ maxQuarter);
System.out.println("Min quarter rainfall = " + minQuarter);
}//end main
}//end class
With these changes, it runs fine for my basic tests:
Enter rainfall for quarter 1: 2
Enter rainfall for quarter 2: 4
Enter rainfall for quarter 3: 6
Enter rainfall for quarter 4: 7
Total rainfall = 19.0
Average rainfall = 4.75
Max quarter rainfall = 7.0
Min quarter rainfall = 2.0
Max quarter rainfall = 4
Minquarter rainfall = 1
You should format your code (Ctrl+Shift+f in Eclipse).
Keep the i index in parallel to the min and max, something like imax, imin. Otherwise you lose the origin of the values like in your case.
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.