No matter what I enter in for my amounts, it always tells me that the total is 13. Here is my code:

String input = "";
		int month = 1;
		int year = 0;
		int total = 0;
		double average = 0.00;
		int inches = 0;
		int rainfall = 0;

		for (int lcv = 0; lcv <= year; lcv++)
		{	// Begin for loop for years
			// Prompt for number of years
			input = JOptionPane.showInputDialog("How many years of rainfall?  ");

			for (int m = 1; month <= 12; month++)
			{	// Begin for loop for months
				// Prompt for inches for the month
				input = JOptionPane.showInputDialog("How many inches of rainfall for the month?  ");

				// Convert input into rainfall
					rainfall = Integer.parseInt(input);

				// Add total rainfall to total
					total += rainfall;

			} // End for loop for months
		}	// End for loop for years

What am I missing here?

Recommended Answers

All 3 Replies

I don't see any output. How is it telling you the total?

The loop that is supposed to go for some number of years will only every go one time because year is set to 0 and lcv is also set to 0.

I do have the system printing out the output, I just didn't include it in my made code part. Here is what I formally have:

String input = "";
		int month = 1;
		int year = 0;
		int total = 0;
		double average = 0.00;
		int inches = 0;
		int rainfall = 0;

		for (int lcv = 0; lcv <= year; lcv++)
		{	// Begin for loop for years
			// Prompt for number of years
			input = JOptionPane.showInputDialog("How many years of rainfall?  ");

			for (int m = 1; month <= 12; month++)
			{	// Begin for loop for months
				// Prompt for inches for the month
				input = JOptionPane.showInputDialog("How many inches of rainfall for the month?  ");

				// Convert input into rainfall
					rainfall = Integer.parseInt(input);

				// Add total rainfall to total
					total += rainfall;

			} // End for loop for months
		}	// End for loop for years

		// Avereage total rainfall
		average = total / month;

		JOptionPane.showMessageDialog(null, "This is the amount of rain fallen per month:  " + month);
		JOptionPane.showMessageDialog(null, "Inches of rainfall is " + input + " per year, and the average is " + average);

	// Exit system
	System.exit(0);

When I enter in, for example, 1 for the number of years, it then takes me to enter in the amount of rain for all the months and goes through that. However, when I enter in the amounts, such as all 0's for the amounts each month, it prints 13 as the total number of inches.

you're using the wrong variables as output
after the loop, the value for month will always be the same. you should replace that 'month' by 'average'
your other problem is a similar one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.