开发者

Really confused on how to extract a value from an if statement

So basically I have this if statement:

int md; // md = marriage deduction
    if (married == 'M'){
    md = 750;
    System.out.println("Deduction for Being Married: " + md);
    }开发者_如何学C
    else if (married == 'S'){
    md = 500;
    System.out.println("Deduction for Being Single: " + md);
    }

And I'm really confused about how to basically extract the value of md from that if statement. I have to, right after this if statement, calculate another integer based on the value of md, but when I try to do so, md is undefined and shows up as an error. Like this:

int total = balance - md - ad

Balance and ad work fine because I didn't have to use if statements for them, but md won't have a value. The error says that it's undefined which I get because I never initialized it outside of the if statement, I'm just wondering how to get the value of md out of the if statements. Thank you so much for help.


md is undefined because when you declare it, you don't set its value. If married is not M or S, then md goes undefined. Otherwise, md should retain whatever value you assign it because it is outside of the if statement's scope.

Just add an else clause to set md to zero:

if (married == 'M')
{
    md = 750;
    System.out.println("Deduction for Being Married: " + md);
}
else if (married == 'S')
{
    md = 500;
    System.out.println("Deduction for Being Single: " + md);
}
else
{
    md = 0;
    // TODO: System.out.println("Oops. You are neither married nor single.");
}

Alternatively, you could just initialize your md to zero. I recommend doing both:

int md = 0;


I would add an adendum to these answers.

if (married == 'M') {
    md = 750;
    System.out.println("Deduction for Being Married: " + md);
} else if (married == 'S') {
    md = 500;
    System.out.println("Deduction for Being Single: " + md);
} else {
    md = 0;
    // what if we later add functionality to add divorced or widowed?
    // would this really be the desired functionality to set md to 0
    // better to throw an exception saying we don't recognise the value of married
    // eg. 
    throw new IllegalStateException("unrecognised married state: "+married);
}

Alternatively you might want to explore using enums

public enum Married {

    M("married", 750),
    S("single", 500);

    private String name;
    private int points;

    Married(String name, int points) {
        this.name = name;
        this.points = points;
    }

    public String getName() { return name; }
    public int getPoints() { return points; }

}

String input = // get input somehow

Married status = Married.valueOf(input);
// status is either M or S now. It cannot be null as valueOf throws an exception if it 
// cannot match the input to string to any of the possible values of the enum

int total = balance - status.getPoints() - ad;


Your value for married is almost certainly not M or S, thus you're hitting neither branch of your statement and md is never being defined.


if married isn't 'M' or 'S', then md will be undefined.


You have a condition where married is neither S nor M... so add a final else to set it to zero.

if (married == 'M'){
md = 750;
System.out.println("Deduction for Being Married: " + md);
}
else if (married == 'S'){
md = 500;
System.out.println("Deduction for Being Single: " + md);
}
else {
md = 0;
}

Or do it before the entire if statement...

Or you have lower case s or m which fails a case sensitive condition


Assuming this is JAVA we are talking about and married is a String object You should compare them by using .equalsTo:

if (married.equalsTo("M")) {...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜