reset password
Author Message
jgarc629
Posts: 76
Posted 15:34 Feb 12, 2017 |

When I type something like 4000 for my input, I get 66 minutes and 40 seconds instead of 1 hour 6 minutes and 40 seconds. I have an else if statement to find the number of hours but it's not being read since the first condition to find the number of minutes still applies. I saw the example MultiWayIfGrading.java and saw there was something called char used. Is that what stopped one condition from overlapping with another one? 

Last edited by jgarc629 at 15:35 Feb 12, 2017.
kknaur
Posts: 540
Posted 16:02 Feb 12, 2017 |
jgarc629 wrote:

When I type something like 4000 for my input, I get 66 minutes and 40 seconds instead of 1 hour 6 minutes and 40 seconds. I have an else if statement to find the number of hours but it's not being read since the first condition to find the number of minutes still applies. I saw the example MultiWayIfGrading.java and saw there was something called char used. Is that what stopped one condition from overlapping with another one? 

Can you be more specific without writing code related to the homework solution?  Otherwise maybe this will answer your question:

If you have a linked if/else if structure as in:

if(condition 1):
  do something
else if (condition 2):
  do something
else if (condition 3):
  do something

 

Here the logic will be executed from top to bottom.  Only one condition is allowed to be true and that is the condition that is executed.  Whichever condition is true first is the condition to execute, the rest of the conditions are ignored. 

Now you could also have logic like the following using separate if statements:

if(condition 1):
  do something

if (condition 2):
  do something

if (condition 3):
  do something

In this case, you have three different if structures and each condition will be tested regardless of the results of any previous condition.  Here if all of the conditions happen to be true, then all the statements under the conditions will execute.

Maybe this helps a bit?

Last edited by kknaur at 16:03 Feb 12, 2017.
jgarc629
Posts: 76
Posted 21:46 Feb 12, 2017 |

My first if statement has the condition if the input is greater than or equal to 60 seconds. My second is an else if statement that has the condition if the input is greater than or equal to 3600. If the input is 3600, then it would say 60 minutes and not 1 hour(s). It seems to stop at the first if statement and would ignore all the other else if statements. I'm not sure why.

sdo8
Posts: 54
Posted 22:05 Feb 12, 2017 |
jgarc629 wrote:

My first if statement has the condition if the input is greater than or equal to 60 seconds. My second is an else if statement that has the condition if the input is greater than or equal to 3600. If the input is 3600, then it would say 60 minutes and not 1 hour(s). It seems to stop at the first if statement and would ignore all the other else if statements. I'm not sure why.

This is when you're going to have to use if statements within if statements. Instead of using the & symbol to have 2 conditions within the if statement, you can have multiple if statements within an if statement.

example:

double x = 10

if ( x < 100 ) { if ( x = 10 ) { do this } else { do this } } else if ( condition 2 ) { do this } else { do this }

Sorry for the formatting

 

jgarc629
Posts: 76
Posted 23:09 Feb 12, 2017 |

Okay I tried doing that but only the last if and else combo comes out the way it should be. I have it structured like this:

if (do something) {

}

else {

}

if (do something) {

else {

}

if (do something) {

else {

}

 

but only the last one ends up coming out the way I want it. The others show up but still carry the same result from the previous condition and add it to the new condition. I am really confused,

sdo8
Posts: 54
Posted 23:42 Feb 12, 2017 |

The problem you're having is that every IF statement is satisfied when you put in a number greater than like 86400. That's because your IF conditions aren't nested and don't include more restrictions with more IF statements. They are all separate from each other. You have to nest the if statements to essentially create more requirements for the code to be run. The if statement doesn't set the requirements for the entire program. The if statement sets the requirements for what is needed for the code between the brackets to be run. That's why there's an else statement. So when there are no satisfactory conditions for the IF statement, then the code after the brackets in the else statement run. 

You have a chocolate bar. You want to know if it is open. If it is open, then you want to know if it is melted. If it is melted, then don't eat it. If it's not melted, then eat it. If it's not opened, save it for later. If it's not open, forget about it being melted or in good condition.

Assume you have an open chocolate bar that is in good condition.

If (chocolate bar is open) { If (chocolate bar is in good condition) { eat it } else { put it away and save it for later } } else { the chocolate bar is closed, save it for later. }

//The first if statement checks to see if the chocolate bar is open, so it is, which is true.

//The second if statement within the first if statement checks to see if the chocolate bar is melted or in good condition. If it's in good condition, then eat it. If its not, then don't eat it (ELSE).

//Finally if the chocolate bar is not open (FIRST IF statement is FALSE) then save the chocolate bar for later (ELSE)

The code should tell you to eat the chocolate, because it's open, not closed (IF/ELSE 1); and it's in good condition,  not melted (IF/ELSE 2). IF 1 is TRUE, and IF 2 is TRUE.

 

Last edited by sdo8 at 23:54 Feb 12, 2017.