943,982 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2205
  • Java RSS
Oct 23rd, 2006
0

If

Expand Post »
Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90 .


if(outsdieTemperature > 90){
shelfLife-4;
}

something is wrong..can you help..?
THK
Reputation Points: 10
Solved Threads: 0
Newbie Poster
THK is offline Offline
15 posts
since Oct 2006
Oct 23rd, 2006
0

Re: If

I strongly suggest that you read through the tutorial.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 23rd, 2006
0

Re: If

Click to Expand / Collapse  Quote originally posted by DavidRyan ...
I strongly suggest that you read through the tutorial.


i'll try ..!
THK
Reputation Points: 10
Solved Threads: 0
Newbie Poster
THK is offline Offline
15 posts
since Oct 2006
Oct 23rd, 2006
0

Re: If

if(outsideTemeratures > 90){
shelfLife=shelfLife-4;
}


i have to use the outsideTemperature variable

i dont understand...
THK
Reputation Points: 10
Solved Threads: 0
Newbie Poster
THK is offline Offline
15 posts
since Oct 2006
Oct 23rd, 2006
0

Re: If

Let's have a look at what is happening in the two blocks you have posted then.
Block 1:
Java Syntax (Toggle Plain Text)
  1. if(outsdieTemperature > 90){
  2. shelfLife-4;
  3. }
Block 2:
Java Syntax (Toggle Plain Text)
  1. if(outsideTemeratures > 90){
  2. shelfLife=shelfLife-4;
  3. }
In Block 1, you are saying if the temperature is above 90 degrees, subtract 4 (I'll assume days) days from the shelflife.Meanwhile, in Block 2, you are saying if the temperature is above 90 degrees, subtract 4 days from the shelflife and let that value be called shelflife from now on.

IE, the outside temperature is 92 degrees and the current value of shelflife is 30 days. After Block 1, shelflife is still equal to 30 days, because you haven't told the program to actually change the value stored in the variable. After Block 2 however, you have told the program to change the value stored in the variable, and shelflife is now equal to 26 (IE it's previous value minus 4 days).
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 23rd, 2006
0

Re: If

i understand that its missing a variable..
i cant find which variable to use....
THK
Reputation Points: 10
Solved Threads: 0
Newbie Poster
THK is offline Offline
15 posts
since Oct 2006
Oct 23rd, 2006
0

Re: If

Click to Expand / Collapse  Quote originally posted by THK ...
i understand that its missing a variable..
i cant find which variable to use....
Have you initialised the variable Shelflife before the if statement?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 23rd, 2006
0

Re: If

yes

int outsideTemperature = 80, shelfLife=10;
THK
Reputation Points: 10
Solved Threads: 0
Newbie Poster
THK is offline Offline
15 posts
since Oct 2006
Oct 23rd, 2006
0

Re: If

There are now 2 possibilities. I'll get to them after a bit of background.

If the variable shelf life isn't being changed, look at the conditional (the bit after If which tells the program the circumstances in which to run the statements in the {}).

In this case, the conditional is (outsideTemeratures > 90). If this evaluates as True, the variable shelfLife will be changed, if it evaluates as False, the variable shelfLife will not be changed.

Now, the possibilities. The first is that you made a typo when you wrote the If block. You have three different spellings of what seems to be the same variable (outsideTemperature in the declaration; outsideTemeratures in the second try at the If block and outsdieTemperature in the first try). Check that your spelling of each variable is the same throught the program.

If the program still doesn't change the value of shelfLife after you have checked your spelling, then answer me this:
Is the temperature outside actually greater than 90 degrees?
You have initialized the temperature variable as 80 degrees, and that is lower than 90 degrees. If the value of the outsideTemperature is not changed between the initialisation and the If block then the If block's conditional (see paragraph 3) will evaluate as False (ie the temperature outside is not greater than 90 degrees) and the statements inside the If block will be skipped.
Last edited by DavidRyan; Oct 23rd, 2006 at 9:35 pm.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Rubiks cubes little brother (lol)
Next Thread in Java Forum Timeline: Please Helppp..i Really Need To Pass The Coursework.its Due Soon





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC