设置方法并递增 [英] set method and incrementing

查看:79
本文介绍了设置方法并递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置新部分时,我需要跟踪计数器.例如,如果m1是Brass,而我使用setSection(Strings);我想要黄铜和Strings ++,但是我不确定语句如何处理,而且不确定getSection()toString()是否能使我获得原始节

When setting new section I need to keep track of the counters. for example if m1 is Brass and I use setSection(Strings); I want it to Brass-- and Strings++ But I''m not sure how to do it with if statments and im not sure if getSection()toString() would get me the original section or not

/**This function sets a musician's Orchestra Section. 
 @param section  is a SymphonySection indicating the musician's Orchestra Section.*/

 public void setSection(SymphonySection section) {
    this.section = section;

    if (getSection().toString().equals( "Strings" )){
        Strings--;
    }
    else if (getSection().toString().equals( "Brass" )){
        Brass--;
    }
    else if (getSection().toString().equals( "Conductor" )){
        Conductor--;
    }
    else if (getSection().toString().equals( "Percussion" )){
        Percussion--;
    }
    else if (getSection().toString().equals( "WoodWinds" )){
        WoodWinds--;
    }

    if (section.toString().equals( "Strings" )){
        Strings++;
    }
    else if (section.toString().equals( "Brass" )){
        Brass ++;
    }
    else if (section.toString().equals( "Conductor" )){
        Conductor ++;
    }
    else if (section.toString().equals( "Percussion" )){
        Percussion ++;
    }
    else if (section.toString().equals( "WoodWinds" )){
        WoodWinds ++;
    }

} 

推荐答案

-不要先使用大写字母命名变量.这些仅限于类名.而是在整数值的前面放置一个"i":iStringiBrass.

-如果可以(这是家庭作业吗?),将对象部分"插入到enum中.
Enum Strings安全得多.

您的问题:

- do not name variables with captial letter first. Those are limited to class names. Instead, place an "i" in front for int values: iString, iBrass.

- If you can (is this homework?) male the Object "section" into an enum.
Much safer to work with Enum than Strings.

Your Problem:

// lower all and raise the selection therefor by 2
iStrings--;
iBrass--;
iConductor--;
iPercussion--;
iWoodWinds--;
if (section.toString().equals( "Strings" )){
  iStrings+=2;
}
else if (section.toString().equals( "Brass" )){
  iBrass+=2;
}
else if (section.toString().equals( "Conductor" )){
  iConductor+=2;
}
else if (section.toString().equals( "Percussion" )){
  iPercussion+=2;
}
else if (section.toString().equals( "WoodWinds" )){
iWoodWinds+=2;
}



但是您需要拆分代码,因此无法使用同一方法同时进行增加和减少.
...除非您有一个值指示它是增加还是减少:



but you need to split the code, you will not be able to make both, increase and decrease in the same method.
...Unless you have a value indicating wether it is increase or decrease:

enum DIR{
	INCREASE,
	DECREASE
}

public void setSection(SymphonySection section, DIR direction) {
if(dircetion.equals(DIR.INCREASE){
  // increase
}
else{
  // decrease
}
}



如果不允许使用enum ,则还可以使用boolean int 值(-1和1).



if enum is not allowed you could also use a boolean or int value (-1 and 1) for that.


这篇关于设置方法并递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆