如何减少if语句的长列表? [英] How can I reduce this long list of if statements?

查看:99
本文介绍了如何减少if语句的长列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里我有一长串的if语句,它们应该检测 int [] anArray; 的值是否在一定范围内。 anArray = new int [15]; int [] anArray; 的值,从 anArray [0] 是:
49 50 51 59 0 5 9 10 15 19 50 55 89 99 100

So here I have this long line of if statements, that are supposed to detect if the value of int[] anArray; is within a certain range. anArray = new int[15]; The values of int[] anArray;, starting from anArray[0] are: 49 50 51 59 0 5 9 10 15 19 50 55 89 99 100

这是确定给定值是否在范围内的代码部分:

This is the part of the code that determines if the given values are within a range:

int[] counterarray = new int[10];
    for (x = 14; x >= 0; x--)
    {
      System.out.println(anArray[x]);
      if (anArray[x] >= 0 && anArray[x] < 10)
      {
        counterarray[0] = counterarray[0] + 1;

      }
      if (anArray[x] >= 10 && anArray[x] < 20)
      {
        counterarray[1] = counterarray[1] + 1;

      }
      if (anArray[x] >= 20 && anArray[x] < 30)
      {
        counterarray[2] = counterarray[2] + 1;

      }
      if (anArray[x] >= 30 && anArray[x] < 40)
      {
        counterarray[3] = counterarray[3] + 1;

      }
      if (anArray[x] >= 40 && anArray[x] < 50)
      {
        counterarray[4] = counterarray[4] + 1;

      }
      if (anArray[x] >= 50 && anArray[x] < 60)
      {
        counterarray[5] = counterarray[5] + 1;

      }
      if (anArray[x] >= 60 && anArray[x] < 70)
      {
        counterarray[6] = counterarray[6] + 1;

      }
      if (anArray[x] >= 70 && anArray[x] < 80)
      {
        counterarray[7] = counterarray[7] + 1;

      }
      if (anArray[x] >= 80 && anArray[x] < 90)
      {
        counterarray[8] = counterarray[8] + 1;

      }
      if (anArray[x] >= 90 && anArray[x] < 101)
      {
        counterarray[9] = counterarray[9] + 1;

      }
    }
    System.out.println("counterarray[0] is " +counterarray[0]);
    System.out.println("counterarray[1] is " +counterarray[1]);
    System.out.println("counterarray[2] is " +counterarray[2]);
    System.out.println("counterarray[3] is " +counterarray[3]);
    System.out.println("counterarray[4] is " +counterarray[4]);
    System.out.println("counterarray[5] is " +counterarray[5]);
    System.out.println("counterarray[6] is " +counterarray[6]);
    System.out.println("counterarray[7] is " +counterarray[7]);
    System.out.println("counterarray[8] is " +counterarray[8]);
    System.out.println("counterarray[9] is " +counterarray[9]);

是的,这就是代码,但if语句的长列表似乎有点多余。 for循环翻转每个数组值并确定它们属于哪个范围。然后 int [] counterarray 将值的总和加起来。那么如何让那长长的if语句列表在美学上更令人愉悦?

Yeah so that's the code, but that long list of if statements seems a bit redundant. The for loop flips through each of the array values and determines what range they belong in. Then the int[] counterarray adds up the amount of values together. So how do I make that long list of if statements more aesthetically pleasing?

推荐答案

int[] counterarray = new int[10];
for (x = 14; x >= 0; x--)
{
  if (anArray[x] >= 0 && anArray[x] < 101) {
    int idx = Math.min(anArray[x] / 10, 9);
    ++counterarray[idx];
  }
}

如果所有范围都是10的倍数(例如0 -9,10-19,20-29等),然后我们可以做一个简单的除以10来得到 counterarray 的索引。 Math.min 部分用于处理奇数的最后一个案例,其(原始)范围为90-100;在100的情况下,idx将等于10,但 Math.min 会对其进行限制,使其不会成为数组的越界索引。

If all the ranges were multiples of 10 (e.g. 0-9, 10-19, 20-29, etc), then we could just do a simple divide by 10 to get the index into counterarray. The Math.min part is to handle the odd last case which has the (original) range of 90-100; in the case of 100, idx would be equal to 10, but the Math.min clamps it so that it won't be an out-of-bounds index into the array.

如果检查是为了确保我们只查看我们期望的范围内的值(在此案例0-100)。否则,我们可能错误地将最后一个桶增加为大值(例如200)。

The if check is to make sure we only look at values within the range that we expect (in this case 0-100). Otherwise we might erroneously increment the last bucket for large values (e.g. 200).

这篇关于如何减少if语句的长列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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