对于如何从 if 语句中提取值感到非常困惑

发布于 2024-11-14 16:25:18 字数 569 浏览 1 评论 0原文

所以基本上我有这个 if 语句:

int md; // md = marriage deduction
    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);
    }

我真的很困惑如何从该 if 语句中提取 md 的值。我必须在 if 语句之后根据 md 的值计算另一个整数,但是当我尝试这样做时,md 未定义并显示为错误。像这样:

int total = balance - md - ad

Balance 和 ad 工作正常,因为我不必为它们使用 if 语句,但 md 不会有值。该错误表明它是未定义的,因为我从未在 if 语句之外初始化它,我只是想知道如何从 if 语句中获取 md 的值。非常感谢您的帮助。

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);
    }
    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

强者自强 2024-11-21 16:25:18

md 未定义,因为当您声明它时,您没有设置它的值。如果 married 不是 MS,则 md 未定义。否则,md 应保留您分配给它的任何值,因为它超出了 if 语句的范围。

只需添加一个 else 子句即可将 md 设置为零:

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.");
}

或者,您也可以将 md 初始化为零。我建议两者都做:

int md = 0;

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;
百思不得你姐 2024-11-21 16:25:18

我会为这些答案添加一个附录。

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);
}

或者,您可能想探索使用枚举

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;

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;
记忆で 2024-11-21 16:25:18

你对已婚的价值几乎肯定不是M或S,因此你没有达到你的陈述的任何一个分支,并且md永远不会被定义。

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.

浊酒尽余欢 2024-11-21 16:25:18

如果已婚不是“M”或“S”,则 md 将是未定义的。

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

半枫 2024-11-21 16:25:18

您遇到的情况是 married 既不是 S 也不是 M...因此添加最后一个 else 来设置它为零。

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;
}

或者在整个 if 语句之前执行此操作...

或者您有小写的 sm ,它们不符合区分大小写的条件

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

橙幽之幻 2024-11-21 16:25:18

假设这是我们正在讨论的 JAVA,并且 ma​​rried 是一个 String 对象,您应该使用 .equalsTo 来比较它们:

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

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")) {...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文