有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对如何从if语句中提取值感到困惑

所以基本上我有一个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的值。非常感谢你的帮助


共 (6) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    我想在这些答案上加上一句话

    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;
    
  3. # 3 楼答案

    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;
    
  4. # 4 楼答案

    你的已婚价值几乎肯定不是M或S,因此你没有触及你陈述的任何一个分支,md也从未被定义

  5. # 5 楼答案

    假设这是我们正在讨论的JAVA,并且是一个字符串对象,您应该使用来比较它们。相等于:

    if (married.equalsTo("M")) {...
    
  6. # 6 楼答案

    您有一个条件,其中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,它不满足区分大小写的条件