Java 简化或减少if语句

Java 简化或减少if语句,java,android-studio,Java,Android Studio,我想用一个开关或一个功能来代替If来减少If的数量,这有可能吗?? 由于我的代码太长,我认为函数比重复的if或else能更好地完成这项工作,而且它也更容易理解 double a = text.charAt(39), b= text.charAt(40), c= text.charAt(41), d= text.charAt(42) ; Collection<Double> list=new ArrayList<Double>(); list

我想用一个开关或一个功能来代替If来减少If的数量,这有可能吗?? 由于我的代码太长,我认为函数比重复的if或else能更好地完成这项工作,而且它也更容易理解

 double a = text.charAt(39), b= text.charAt(40), c= text.charAt(41), d= text.charAt(42) ;
       Collection<Double> list=new ArrayList<Double>();
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        if ( a>=15||b>=15||c>=15||d>=15) {
            if (  Collections.max(list) == a) {
                Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 1");
            }
            else if ( Collections.max(list) == b)
            {
                Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 2");
            }
            else if ( Collections.max(list) == c )
            {
                Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 3");
            }
            else
            {
                Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 4");
            }

            Conseil_detecteur.setText("--> Par mesure de sécurité, nous vous conseillons vivement de vérifier que le détecteur est bien collé au produit à sécuriser.\nPour une adhésion optimale, remplacez l’adhésif.\nVérifiez que le détecteur est bien connecté à la centrale et qu’il est en bon état.");

        }
        else {
            Conseil_detecteur.setText("--> Des alarmes régulières ? Envie d'en savoir plus ?\nContactez notre hotliner au 02 37 33 69 66 qui vous guidera dans leurs résolutions.");
            Defauts_detecteur.setVisibility(View.GONE);
        }
double a=text.charAt(39),b=text.charAt(40),c=text.charAt(41),d=text.charAt(42);
集合列表=新的ArrayList();
列表.添加(a);
列表.添加(b);
增加(c)项;
列表.添加(d);
如果(a>=15 | | b>=15 | | c>=15 | | d>=15){
if(Collections.max(list)==a){
Defauts_detecteur.setText(“1号探测器上的电流常数”);
}
else if(Collections.max(list)==b)
{
Defauts_detecteur.setText(“2号探测器上的电流常数”);
}
else if(Collections.max(list)==c)
{
Defauts_detecteur.setText(“3号探测器上的电流常数”);
}
其他的
{
Defauts_detecteur.setText(“第四个检测器的电流常数”);
}
ContheldDeCeTur.Set Tr.S.Meule de Se CuriTe,Nou-VousConsiion VistreQueLeTeCuTeR。
}
否则{
Conseilèu detecteur.setText(“-->Des alarmes régulières?Envie d'en savoir plus?\n联系notre hotliner au 02 37 33 69 66 qui vous guidera dans leurs résolutions.”);
Defauts_detecteur.setVisibility(View.GONE);
}
始终可以使用循环(或流,具体取决于性能要求):

final double a=text.charAt(39),b=text.charAt(40),c=text.charAt(41),d=text.charAt(42);
最终列表=数组。asList(a,b,c,d);//请注意,数组(double[])而不是集合可以避免装箱
最终双精度最大值=Collections.max(列表);
如果(maxValue>=15){//至少有一个值大于或等于15,则无需单独检查每个元件
对于(int i=0;iDes alarmes régulières?Envie d'en savoir plus?\n联系notre hotliner au 02 37 33 69 66 qui vous guidera dans leurs résolutions.”);
Defauts_detecteur.setVisibility(View.GONE);
}
请注意,如果两个值相同且最大(例如a=16、b=16、c=12、d=9),则当前代码的行为不明确。输出为“检测器1”,但“检测器2”也是最大值

如果您的列表可以包含多个max值,并且希望找到所有max值,则需要稍微更改循环(但您现有的代码只找到“first”值的位置)

final List maxPositions=new ArrayList();
对于(int i=0;i
功能性风格:

    HashMap<Character, Integer> values = new HashMap<>();
    values.put('a', 239);
    values.put('b', 123);
    values.put('c', 345);
    values.put('d', 555);

    Optional<Map.Entry<Character, Integer>> max = values.entrySet().stream().filter(e -> e.getValue() > 15).max(Map.Entry.comparingByValue());
    max.ifPresent(characterIntegerEntry -> System.out.println(" max entry value for detector " + characterIntegerEntry.getKey() + " is" + characterIntegerEntry.getValue()));
HashMap value=newhashmap();
价值。put('a',239);
价值。put('b',123);
价值。put('c',345);
价值。put('d',555);
可选的max=values.entrySet().stream().filter(e->e.getValue()>15.max(Map.Entry.comparingByValue());
max.ifPresent(characterIntegerEntry->System.out.println(“检测器的最大输入值”+characterIntegerEntry.getKey()+“是”+characterIntegerEntry.getValue()));

将处理任意数量的数据。

您的内部
else
真的是else吗?还是应该是
max(list)==d
呢?switch case语句总是希望有一个数字作为参数,switch(id)case 4:…如果if body只包含一条语句,那么您可以编写它,而不需要括号({)如果你的代码正常工作,你最好还是继续问下去。
text.charAt()
给你这个位置的字符,而不是一个双倍值(度量?)。所以
“abc123”。charAt(0)
给你
97
,”。charAt(3)`给出
49
。这是有意的吗?还要注意,通过
=
比较双倍可能不起作用。如果我有两个相同的最大值,在这种情况下如何解决?Antoine我不知道。我不知道你的期望值。你想找到列表中所有最大值的位置吗?还是第一个最大值?还是latest最大值?我想找到大于15的最大值的位置,如果2或3个值等于且大于15,我也想找到它们。你想找到的是位置而不是值,对吗?是的,我想找到max的位置(以及大于15的位置)值。然后我把这个值的位置放在下面的文本中。
switch
不能处理非常量值。这里switch的问题是开关标签必须是文本/常量值。这里不是这种情况
final List<Integer> maxPositions = new ArrayList<>();
for (int i = 0; i < list.size(); ++i) {
  if (list.get(i) == maxValue) {
    maxPositions.add(i+1);
    // don't stop, keep going
  }
}

// will print e.g. "found at positions [1, 4]
….setText("Max values found at positions " + maxPositions);
    HashMap<Character, Integer> values = new HashMap<>();
    values.put('a', 239);
    values.put('b', 123);
    values.put('c', 345);
    values.put('d', 555);

    Optional<Map.Entry<Character, Integer>> max = values.entrySet().stream().filter(e -> e.getValue() > 15).max(Map.Entry.comparingByValue());
    max.ifPresent(characterIntegerEntry -> System.out.println(" max entry value for detector " + characterIntegerEntry.getKey() + " is" + characterIntegerEntry.getValue()));