【问题标题】:Java Octal to Binary Conversion (without predefined methods)Java八进制到二进制转换(没有预定义的方法)
【发布时间】:2016-02-29 07:02:54
【问题描述】:

我正在尝试创建一个将八进制值转换为二进制值的计算器。

temp = txt.getText();
temptemp = "";
if (prev == 3) {
    for (int i = 0; i < temp.length() - 1; i++) {
        if (temp.charAt(i) == '1') {
            temptemp = temptemp + "000";
        } else if (temp.charAt(i) == '2') {
            temptemp = temptemp + "001";
        } else if (temp.charAt(i) == '3') {
            temptemp = temptemp + "010";
        } else if (temp.charAt(i) == '4') {
            temptemp = temptemp + "011";
        } else if (temp.charAt(i) == '5') {
            temptemp = temptemp + "100";
        } else if (temp.charAt(i) == '6') {
            temptemp = temptemp + "101";
        } else if (temp.charAt(i) == '7') {
            temptemp = temptemp + "111";
        }
    }
    temp = temptemp;
    txt.setText(temp);

我的循环语句有什么问题?请帮忙。谢谢你:)

编辑:

我现在知道问题所在了。感谢所有的cmets和答案家伙。我偏离了 1 个增量。我应该从== 0 开始。对不起,谢谢你:)

【问题讨论】:

  • 你的输入是什么,预期输出和实际输出?
  • 第二个temp.charAt(i) == '3' 可能应该是temp.charAt(i) == '7'。你也没有处理'0'
  • 首先检查数学。 '3' 两次,没有零,错误的转换字符串。 (八进制 1 绝对不是二进制 000)
  • 有时没有输出@AndyTurner
  • 我明白了。谢谢@cadrian

标签: java string for-loop


【解决方案1】:

对于您的情况,这是正确的转换者。 您可以查看转换here

for (int i = 0; i < temp.length() - 1; i++) {
   if (temp.charAt(i) == '0') {
         temptemp = temptemp + "000";
   } else if (temp.charAt(i) == '1') {
        temptemp = temptemp + "001";
   } else if (temp.charAt(i) == '2') {
        temptemp = temptemp + "010";
   } else if (temp.charAt(i) == '3') {
        temptemp = temptemp + "011";
    } else if (temp.charAt(i) == '4') {
        temptemp = temptemp + "100";
    } else if (temp.charAt(i) == '5') {
        temptemp = temptemp + "101";
    } else if (temp.charAt(i) == '6') {
         temptemp = temptemp + "110";
    } else if (temp.charAt(i) == '7') {
        temptemp = temptemp + "111";
   }
}

相反,您可以循环遍历数字,每次将数字除以 2,在循环内通过% 2(或使用&gt;&gt;)检查他的结果,如果它是0,则将数字添加到结果中0,否则1

【讨论】:

    【解决方案2】:

    您在转换表中犯了几个错误。 (没有 0 和没有 7,错误的转换,通过过早停止循环来省略最后一个字符)。

    在更正之后,您应该考虑使用StringBuilder 而不仅仅是连接字符串! switch 语句可能比 if-else 链更具可读性

    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < temp.length(); i++) {
        switch(temp.charAt(i)) {
        case '0':
            strBuilder.append("000");
            break;
        case '1':
            strBuilder.append("001");
            break;
        case '2':
            strBuilder.append("010");
            break;
        case '3':
            strBuilder.append("011");
            break;
        case '4':
            strBuilder.append("100");
            break;
        case '5':
            strBuilder.append("101");
            break;
        case '6':
            strBuilder.append("110");
            break;
        case '7':
            strBuilder.append("111");
            break;
        }
    }
    String temptemp = strBuilder.toString();
    

    还有另一种可能性要短得多(而且完全不可读;)):

    String[] convArray = { "000", "001", "010", "011", "100", "101", "110", "111" };
    StringBuilder strBuild = new StringBuilder();
    for (int i = 0; i < temp.length(); i++)
        strBuild.append(convArray[temp.charAt(i)-48]);
    String temptemp = strBuild.toString();
    

    请注意,这只有在字符串真的只包含数字 0-7 时才有效

    【讨论】:

      【解决方案3】:

      不用考虑所有 8 个分支也很容易做到

      for (int i = 0; i < temp.length(); i++) {
        int d = Character.getNumericValue(temp.charAt(i));                           
        for (int k=2; k >= 0; k--)
          temptemp = temptemp + Character.forDigit((i >> k) & 1, 2);
      }
      

      【讨论】:

        【解决方案4】:

        您可以通过执行一些按位运算来跳过这一长长的显式案例列表:

        StringBuilder sb = new StringBuilder(3 * temp.length());
        for (int i = 0; i < temp.length(); i++) {
          // + check that the character is actually an octal digit.
          int digit = Character.digit(temp.charAt(i), 10);
          for (int b = 2; b >= 0; --b) {
            sb.append(Character.forDigit((digit >> b) & 0x1, 10));
          }
        }
        

        【讨论】:

          【解决方案5】:
          private static String octalToBinary(int octal) {
                  if (octal < 1 || octal > 7) {
                      throw new IllegalArgumentException("Not an ocatl number");
                  }
          
                  String binary = new String();
                  int myOctal = octal / 2;
                  for (; myOctal >= 1;) {
                      binary = octal % 2 + binary;
                      octal = myOctal;
                      myOctal = myOctal / 2;
                  }
                  binary = octal + binary;
          
                  // if you want to make it of length 3
                  if (binary.length() == 1) {
                      binary = "00" + binary;
                  }
          
                  if (binary.length() == 2) {
                      binary = "0" + binary;
                  }
                  return binary;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-28
            • 1970-01-01
            • 2021-01-29
            • 1970-01-01
            • 2015-12-12
            • 2014-06-10
            • 2017-09-27
            相关资源
            最近更新 更多