Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-04-2011, 07:09 PM   #1
Solid State Member
 
Join Date: Sep 2010
Posts: 17
Default Java if-else question

I'm trying to code a program to calculate the average rainfalls over a year. This is my code:

import java.util.Scanner;
public class C_C_PartC
{
public static void main(String[]args)
{
//title
System.out.println("**This is Craig Copeland's Rainfall Averager**");
System.out.println("This program will calculate the average rainfall over the year\n");

//create Scanner
Scanner input = new Scanner(System.in);

//create variables
int spring = 0;
int summer = 0;
int autumn = 0;
int winter = 0;
double averageRainfall = 0.0;
int totalRainfall = 0;

//prompt users for input
System.out.print("Enter your rainfall for spring...");
spring = input.nextInt();

System.out.print("Enter your rainfall for summer...");
summer = input.nextInt();

System.out.print("Enter your rainfall for autumn...");
autumn = input.nextInt();

System.out.print("Enter your rainfall for winter...");
winter = input.nextInt();

//calculate average
totalRainfall = spring + summer + autumn + winter;
averageRainfall = totalRainfall / 4;

// if-else statement and final printout
if(averageRainfall >=30.0)
{
System.out.println("Your average monthly rainfall is "+ averageRainfall +" mm of rain...need an umbrella?");
}
else if(averageRainfall >=20.0)
{
System.out.println("Your average monthly rainfall is "+ averageRainfall +" mm of rain...this is good for the garden!");
}
else if(averageRainfall >=10.0)
{
System.out.println("Your average monthly rainfall is "+ averageRainfall +" mm of rain...your lawn is looking thirsty.");
}
else
{
System.out.println("Your average monthly rainfaill is "+ averageRainfall +" mm of rain... you must drive a camel");
}



}//end main method
}//end class

Now that runs just fine, but I want to make the if-else more specific such as:
else if(averageRainfall >=10.0 && <20.0)

Now when I did that, the program would not compile and I had about 20+ errors. Something to do with double and boolean variables not being allowed to use the and(&&) and or(||) operators in an if-else statement.

Any help would be great!
Thanks!
__________________

copelandtml is offline   Reply With Quote
Old 02-05-2011, 08:19 AM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java if-else question

You have to specify the variable both times like this:

Code:
else if(averageRainfall >=10.0 && averageRainfall <20.0)
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 02-05-2011, 03:10 PM   #3
Solid State Member
 
Join Date: Sep 2010
Posts: 17
Default Re: Java if-else question

Ok, thanks for the reply...kinda feel like an idiot now lol but I guess now I know for next time. Is that only for double and boolean variables or is that for all variables?
copelandtml is offline   Reply With Quote
Old 02-05-2011, 03:16 PM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java if-else question

True for everything
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 02-08-2011, 03:09 PM   #5
Solid State Member
 
Join Date: Sep 2010
Posts: 17
Default Re: Java if-else question

alright, thanks again....another quick question if you don't mind.
when creating variables is it good to initialize them to something (0 or "") such as:
int number = 0;
String name = "";
copelandtml is offline   Reply With Quote
Old 02-08-2011, 06:28 PM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java if-else question

Generally yes. If it's a local variable you can't do anything without initialising it anyway, if it's a field it'll automatically get set to a default value (but it's better to explicitly define this to state your intent.)
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 04:23 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0