Java 减少if语句的数量,或替换为for或switch或其他语句

Java 减少if语句的数量,或替换为for或switch或其他语句,java,if-statement,Java,If Statement,我的if和else if条件太长。是否可以在或切换时为或设置一个,或任何其他可以减少代码并使其更易于理解的功能 除此之外,代码运行良好 if((text.charAt(7)=='O' || text.charAt(7)=='X') && (text.charAt(8)=='O' || text.charAt(8)=='X') && (text.charAt(9)=='O' || text.charAt(9)=='X')

我的
if
else if
条件太长。是否可以在或切换时为或设置一个
,或任何其他可以减少代码并使其更易于理解的功能

除此之外,代码运行良好

if((text.charAt(7)=='O' || text.charAt(7)=='X')
        && (text.charAt(8)=='O' || text.charAt(8)=='X')
        && (text.charAt(9)=='O' || text.charAt(9)=='X') 
        && (text.charAt(10)=='O'|| text.charAt(10)=='X')
        && text.charAt(49)=='O'
        && text.charAt(50)=='O'
        && text.charAt(51)=='X')
 {
    ledGeneral.setBackgroundResource(R.drawable.ledverte);
}else if (text.charAt(7)=='A' 
        || text.charAt(8)=='A'
        || text.charAt(9)=='A'
        || text.charAt(10)=='A'
        || text.charAt(49)=='A'
        || text.charAt(50)=='A')
{
    ledGeneral.setBackgroundResource(R.drawable.ledgeneralrouge);
}else{
    ledGeneral.setBackgroundResource(R.drawable.imageverte);
}

您可以尝试以下方法,尽管我怀疑这是否会使其更具可读性:

final boolean firstCondition = IntStream.of(7, 8, 9, 10)
        .mapToObj(text::charAt)
        .allMatch(character -> character == 'O' || character == 'X')
          && text.charAt(49) == 'O'
          && text.charAt(50) == 'O'
          && text.charAt(51) == 'X';
final boolean secondCondition = IntStream.of(7, 8, 9, 10, 49, 50)
        .mapToObj(text::charAt)
        .anyMatch(character -> character == 'A');

if (firstCondition) {
    //
} else if (secondCondition) {
    //
} else {
    //
}

您可以尝试以下方法,尽管我怀疑这是否会使其更具可读性:

final boolean firstCondition = IntStream.of(7, 8, 9, 10)
        .mapToObj(text::charAt)
        .allMatch(character -> character == 'O' || character == 'X')
          && text.charAt(49) == 'O'
          && text.charAt(50) == 'O'
          && text.charAt(51) == 'X';
final boolean secondCondition = IntStream.of(7, 8, 9, 10, 49, 50)
        .mapToObj(text::charAt)
        .anyMatch(character -> character == 'A');

if (firstCondition) {
    //
} else if (secondCondition) {
    //
} else {
    //
}

关于代码“清洁度”的问题,我建议用描述性的方法来表达你的观点,例如

private boolean shouldSetBackgroundLedverte(text){
  return (text.charAt(7)=='O' || text.charAt(7)=='X') && (text.charAt(8)=='O'|| 
         text.charAt(8)=='X')&& (text.charAt(9)=='O'|| text.charAt(9)=='X') && 
         (text.charAt(10)=='O'|| text.charAt(10)=='X')&& text.charAt(49)=='O'&& 
         text.charAt(50)=='O'&& text.charAt(51)=='X';
}

private boolean shouldSetBackgroundLedgeneralrouge(text){
  return text.charAt(7)=='A' || text.charAt(8)=='A'|| text.charAt(9)=='A'|| 
         text.charAt(10)=='A'|| text.charAt(49)=='A'|| text.charAt(50)=='A'
}

private setBackground(String text) {
  if (shouldSetBackgroundLedverte(text)) {
    ledGeneral.setBackgroundResource(R.drawable.ledverte);
  } else if (shouldSetBackgroundLedgeneralrouge(text)) {
    ledGeneral.setBackgroundResource(R.drawable.ledgeneralrouge);
  } else {
    ledGeneral.setBackgroundResource(R.drawable.imageverte);
  }
}

关于代码“清洁度”的问题,我建议用描述性的方法来表达你的观点,例如

private boolean shouldSetBackgroundLedverte(text){
  return (text.charAt(7)=='O' || text.charAt(7)=='X') && (text.charAt(8)=='O'|| 
         text.charAt(8)=='X')&& (text.charAt(9)=='O'|| text.charAt(9)=='X') && 
         (text.charAt(10)=='O'|| text.charAt(10)=='X')&& text.charAt(49)=='O'&& 
         text.charAt(50)=='O'&& text.charAt(51)=='X';
}

private boolean shouldSetBackgroundLedgeneralrouge(text){
  return text.charAt(7)=='A' || text.charAt(8)=='A'|| text.charAt(9)=='A'|| 
         text.charAt(10)=='A'|| text.charAt(49)=='A'|| text.charAt(50)=='A'
}

private setBackground(String text) {
  if (shouldSetBackgroundLedverte(text)) {
    ledGeneral.setBackgroundResource(R.drawable.ledverte);
  } else if (shouldSetBackgroundLedgeneralrouge(text)) {
    ledGeneral.setBackgroundResource(R.drawable.ledgeneralrouge);
  } else {
    ledGeneral.setBackgroundResource(R.drawable.imageverte);
  }
}

您可以创建一个辅助函数来处理该问题:

static boolean check_letter(String text, char to_check, int ...pos){
    for (int po : pos) {
        if (text.charAt(po) == to_check)
            return true;
    }
    return false;
}
然后调整if和else:

if((check_letter(text, 'O', 7, 8, 9, 10) || check_letter(text, 'X', 7, 8, 9, 10)) && text.charAt(49) == 'O' && text.charAt(50) == 'O' && text.charAt(51) == 'X')
{
  ...
}
else if (check_letter(text, 'A', 7, 8, 9, 10, 49, 50))
{
 ...
}

您可以创建一个辅助函数来处理该问题:

static boolean check_letter(String text, char to_check, int ...pos){
    for (int po : pos) {
        if (text.charAt(po) == to_check)
            return true;
    }
    return false;
}
然后调整if和else:

if((check_letter(text, 'O', 7, 8, 9, 10) || check_letter(text, 'X', 7, 8, 9, 10)) && text.charAt(49) == 'O' && text.charAt(50) == 'O' && text.charAt(51) == 'X')
{
  ...
}
else if (check_letter(text, 'A', 7, 8, 9, 10, 49, 50))
{
 ...
}

如果要检查的索引是固定的:

if(text.matches(".{7}[OX][OX][OX][OX].{38}OOX.*")){
    ledGeneral.setBackgroundResource(R.drawable.ledverte);
}
else if (text.substring(7, 11).contains("A") || text.substring(49, 51).contains("A")){
    ledGeneral.setBackgroundResource(R.drawable.ledgeneralrouge);
}
else{
    ledGeneral.setBackgroundResource(R.drawable.imageverte);
}
{38}和{7}都是所谓的量词。匹配上一个标记的指定数量。例如:

  • [十] {1,3}将匹配1到3个X
  • {3} 将正好匹配3
  • {3,}将匹配3个或更多
一般来说,[x]{min,max}:将min与max x匹配,[x]{n}精确匹配nx,[x]{min,}至少匹配min x或更多

表示匹配任何字符。所以
{38}
意味着匹配任何字符38次

因为您的情况可以解释为:

匹配一个字符串

  • 从任意7个字符开始(索引0-6)
  • 在第7、第8、第9和第10个指数处为a
    O
    X
  • 以及从第11个到第48个索引的任何字符(38个字符)
  • 第49和第50个指数处的a
    O
  • 第51个索引处的
    X
转换为正则表达式

  • ..
    或更短的
    {7}
    (任意字符正好7次)
  • [OX][OX][OX][OX]
    char类中指定的与索引7,8,9,10相对应的字符之一
  • 或更短的
    {38}
    (任何字符正好38次)
  • 紧随其后的是指数49,50,51处的
    OOX
  • *
    可选结尾处的零个或多个附加字符
因此,长版本可能是:

.......[OX][OX][OX][OX]......................................OOX.*
短的

.{7}[OX][OX][OX][OX].{38}OOX.*

如果要检查的索引是固定的:

if(text.matches(".{7}[OX][OX][OX][OX].{38}OOX.*")){
    ledGeneral.setBackgroundResource(R.drawable.ledverte);
}
else if (text.substring(7, 11).contains("A") || text.substring(49, 51).contains("A")){
    ledGeneral.setBackgroundResource(R.drawable.ledgeneralrouge);
}
else{
    ledGeneral.setBackgroundResource(R.drawable.imageverte);
}
{38}和{7}都是所谓的量词。匹配上一个标记的指定数量。例如:

  • [十] {1,3}将匹配1到3个X
  • {3} 将正好匹配3
  • {3,}将匹配3个或更多
一般来说,[x]{min,max}:将min与max x匹配,[x]{n}精确匹配nx,[x]{min,}至少匹配min x或更多

表示匹配任何字符。所以
{38}
意味着匹配任何字符38次

因为您的情况可以解释为:

匹配一个字符串

  • 从任意7个字符开始(索引0-6)
  • 在第7、第8、第9和第10个指数处为a
    O
    X
  • 以及从第11个到第48个索引的任何字符(38个字符)
  • 第49和第50个指数处的a
    O
  • 第51个索引处的
    X
转换为正则表达式

  • ..
    或更短的
    {7}
    (任意字符正好7次)
  • [OX][OX][OX][OX]
    char类中指定的与索引7,8,9,10相对应的字符之一
  • 或更短的
    {38}
    (任何字符正好38次)
  • 紧随其后的是指数49,50,51处的
    OOX
  • *
    可选结尾处的零个或多个附加字符
因此,长版本可能是:

.......[OX][OX][OX][OX]......................................OOX.*
短的

.{7}[OX][OX][OX][OX].{38}OOX.*