CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Does not show the if statements in any way? (http://www.codingforums.com/showthread.php?t=270228)

dannyboi 08-11-2012 02:38 AM

Does not show the if statements in any way?
 
import javax.swing.JOptionPane;
public class Main{





public static void main(String[] args) {
// declare your variables still
int salesTotal = 5;
double commissionRate = 10;
int salesClass = 10;

if (salesTotal < 10000) {
if (salesClass == 1)
commissionRate = 0.02;
}
else if (salesClass == 2) {
commissionRate = 0.025;
JOptionPane.showMessageDialog(null, "Commission is " + commissionRate+salesClass+salesTotal);
}

}
}

Will not show me the if statements. Is it the fact I forgot to put in the InputMethod?

Fou-Lu 08-11-2012 03:22 AM

Given the salesTotal and salesClass variables you have here, you have no branch to follow which results in any processing with the values provided. You do match salesTotal < 10000, but you do not match salesClass == 1. There is no else or elseif off of this branch, so no further action is taken and you exit the branch. There is no processing instructions beyond this point.

dannyboi 08-11-2012 03:24 AM

So how would I go about branching out?

Fou-Lu 08-11-2012 03:25 AM

I don't understand the question. You would use elseif and else clauses to continue evaluating a branch.

dannyboi 08-11-2012 03:35 AM

No, I mean, for it to compile, because when I compile, its like just compiling the main method. Nothing.

Fou-Lu 08-11-2012 03:40 AM

Yes, but you won't receive any output. You haven't specified a set of branch evaluations that actually lead to any useful processing or output, so it performs no tasks when run.
Look at your branch statements. You have this:
Code:

if (salesTotal < 10000) // this is true
{
    if (salesClass == 1) // this is false
    {
    }
    // you are here.
}
else if (salesClass == 2) // will never get here.
{
    // output
}

Look at where you are there. Since there is no satisfied condition it abandons the inner branch and returns to the outer branch. The outer branch provides no else, so it has no condition to satisfy and continues processing. There is no following instruction.

The only way you will ever get output in this is if salesClass is set to 2.


All times are GMT +1. The time now is 06:32 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.