Can anyone help me with the following java code using \'for\' loops and \'while\
ID: 3627057 • Letter: C
Question
Can anyone help me with the following java code using 'for' loops and 'while' loops? The finished code should output the following:Enter the number of years: 2
Enter the data for year 1
Rainfall for month 1: 2.1
Rainfall for month 2: 3.3
Rainfall for month 3: 3.8
Rainfall for month 4: -5
Invalid entry. Please re-enter the rainfall for month 4: 3.9
Rainfall for month 5: 4.2
Rainfall for month 6: 4.5
Rainfall for month 7: 3.8
Rainfall for month 8: 2.9
Rainfall for month 9: 2.7
Rainfall for month 10: 2.1
Rainfall for month 11: 1.6
Rainfall for month 12: 1.8
Enter the data for year 2
Rainfall for month 1: 2.4
etc, etc, where the outer loop is a 'for' loop keeping track of the user's input value for the number of years, and the inner loop is also a 'for' loop with a running total of user inputted values for the monthly rainfall. A 'while' loop is also nested in the inner 'for' loop to be sure the user has entered positive values for the rainfall. At the end of the program, it tells the number of total months for which the data was collected, the total rainfall, and the average monthly rainfall. Here is a copy of my code I have written so far, and i cannot get it to work and have no idea where i'm going wrong. Help is greatly appreciated.
//declare variables
int year,numYears,numMonth,userYears;
double rainEntered,totalRain,avgRain;
Scanner keyboard = new Scanner(System.in);
//calculations
totalRain = keyboard.nextDouble();
rainEntered = keyboard.nextDouble();
totalRain = totalRain + rainEntered;
numMonth = keyboard.nextInt();
avgRain = totalRain / numMonth;
//get user input
System.out.printf("Please enter the number of years: ");
userYears = keyboard.nextInt();
while(userYears < 1)
{
System.out.println("You must enter at least 1.");
System.out.println("Please re-enter the number of years.");
userYears = keyboard.nextInt();
}
System.out.println("");
year = keyboard.nextInt();
for(numYears = 1; numYears <= userYears; numYears++)
{
for(numMonth = 1; numMonth <= 12; numMonth++)
{
System.out.println("Enter the data for year " + year);
System.out.printf(" Enter the rainfall for month " +
numMonth + " : ");
numMonth = keyboard.nextInt();
}
for(rainEntered = 0; rainEntered > 0; rainEntered++)
while(rainEntered < 0)
{
System.out.println("Invalid entry. Please re-enter the "
+ "rainfall for month " + numMonth);
rainEntered = keyboard.nextDouble();
}
}
System.out.println("Total number of months for which data was "
+ "collected: " + numMonth);
totalRain = keyboard.nextDouble();
System.out.println("Total rainfall: " + totalRain);
avgRain = keyboard.nextDouble();
System.out.println("Average monthly rainfall: " + avgRain);
Explanation / Answer
import java.util.*;
class rainFall
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int year,numYears,numMonth,userYears;
double rainEntered,totalRain,avgRain;
double[] rainfall = new double[100];
int p = 1;
//get user input
System.out.printf("Please enter the number of years: ");
userYears = keyboard.nextInt();
while(userYears < 1)
{
System.out.println("You must enter at least 1.");
System.out.println("Please re-enter the number of years.");
userYears = keyboard.nextInt();
}
for(numYears = 1; numYears <= userYears; numYears++)
{
for(numMonth = 1; numMonth <= 12; numMonth++)
{
System.out.println("Enter the data for year " + numYears);
System.out.printf(" Enter the rainfall for month " + numMonth + " : ");
rainEntered = keyboard.nextDouble();
while(rainEntered < 0)
{
System.out.println("Invalid entry. Please re-enter the "+ "rainfall for month " + numMonth);
rainEntered = keyboard.nextDouble();
}
rainfall[p++]=rainEntered;
}
}
//calculations
totalRain = 0;
for(int k=1; k<p; k++)
totalRain = totalRain + rainfall[k];
avgRain = totalRain / (userYears*12);
System.out.println("Total number of months for which data was " + "collected: " + (p-1));
System.out.println("Total rainfall: " + totalRain);
System.out.println("Average rainfall: " + avgRain);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.