【问题标题】:How to display equivalent octal values for the numbers 1 through 256如何显示数字 1 到 256 的等效八进制值
【发布时间】:2014-09-18 07:58:51
【问题描述】:

当我将数字转换为不同类型时,我正在制作这个程序来帮助我。我有一个数组,它显示 1 到 256 之间的每个数字的所有二进制值,以及一个对所有十六进制值执行相同操作的数组。我遇到的问题是显示八进制值。

当我第一次编写这个程序时,我手动输入了每个数字,然后计算全部在程序中完成并显示转换。现在我决定我只想显示从 1 到 256 的每个数字及其对应的二进制、十六进制和八进制值的表格。

显示二进制和十六进制的值没有问题,但是当我进入八进制时,如果我不使用用户输入,所有内容都会显示为零。添加用户输入后,八进制数组的每个值都与我输入的值相同。

我只需要有人查看我的代码并告诉我我的逻辑哪里出错了。

import java.util.*;
public class NumberConverter 
{
    static int num = 0;
    static String bin = "" + num, hex, oct;

    public static void main (String [] args)
    {

/**
 * Allows for user input to enter a number between 1 & 256 which is then passed to three methods      for conversion into binary, hexadecimal, and octal values     
 */
//          Scanner scan = new Scanner (System.in);
//          System.out.print("Please enter a positive integer between 1 & 256: ");  //prompts the          user for a positive integer between 1 & 256
//          num = scan.nextInt();
//      
//          if(num <= 0 || num > 256)
//          {
//              System.out.println("Invalid number.\n");
//              //if the number entered by the user is 0 or less, or greater than 256, an Invalid message is displayed.
//          }
//          
//          else 
//          {
//              System.out.print("\nDecimal to Binary Conversion: ");
//              NumberConverter myBinary = new NumberConverter();       //creates a new object    called myBinary from the NumberConverter Class
//              System.out.println(myBinary.toBinary(num));             //takes the myBinary     object and modifies it using the toBinary method
//          
//              System.out.print("Binary to Hexadecimal Conversion: ");
//              NumberConverter myHex = new NumberConverter();          //creates a new object called myHex from the NumberConverter Class
//              System.out.println(myHex.binaryToHex("" + num));        //takes the myHex object and modifies it using the binaryToHex method
//      
//              System.out.print("Binary to Octal Conversion: ");
//              NumberConverter myOctal = new NumberConverter();        //creates a new object called myOctal from the NumberConverter Class
//              System.out.println(myOctal.binaryToOctal(num));         //takes the myOctal object and modifies it using the binaryToOctal method
//          }

/**
 * Creates an array to store and display all hexadecimal values for the numbers 1 through 256   
 */
    String [] hexConv = new String [257];
    NumberConverter myHex = new NumberConverter();
    for(int count = 1; count < hexConv.length; count++)
    {
        hexConv[count] = myHex.binaryToHex("" + count);
    }       
/**
 * Creates an array to store and display all Octal values for the binary values of the numbers 1 through 256    
 */
    String [] octConv = new String [257];
    NumberConverter myOctal = new NumberConverter();
    for(int count = 1; count < octConv.length; count++)
    {
        octConv[count] = myOctal.binaryToOctal(num);
    }

/**
 * Creates an array to store and display all binary values for the numbers 1 through 256
 * and prints the contents of all three arrays  
 */
    String [] binConv = new String [257];
    NumberConverter myBinary = new NumberConverter();
    System.out.println("\n\n#\tBinary\t\t   Hexadecimal\t       Octal");
    for(int count = 1; count < binConv.length; count++)
    {
        System.out.print(count + ":\t");
        binConv[count] = myBinary.toBinary(count);
        System.out.println("\t\t\t" + hexConv[count] + "\t\t" + octConv[count]);
        System.out.println("--------------------------------------------------------------");
    }       
}

public String toBinary (int num)
{
    String remainder = "" + num % 2;

    if (num < 1) 
    {
        return "" + num;
    }

    bin = remainder; //remainder value is stored as the value of bin
    toBinary(num >> 1);
    System.out.print(remainder);
    return " ";

}

public String binaryToHex(String binaryValue)
{
    String result = ""; 
    int n = Integer.parseInt(binaryValue);
    int remainder = n % 16;

    if (n == 0) 
    {
        return "";
    } 

    else 
    {
        switch (remainder) 
        {
            case 10:
                result = "A";
                break;
            case 11:
                result = "B";
                break;
            case 12:
                result = "C";
                break;
            case 13:
                result = "D";
                break;
            case 14:
                result = "E";
                break;
            case 15:
                result = "F";
                break;
            default:
                result = remainder + result;
                break;
        }
        hex = binaryToHex(Integer.toString(n / 16)) + result;
        return hex;
    }
}


public String binaryToOctal(int decimalValue)
{
    decimalValue = Integer.parseInt("" + num, 8);
    return "" + decimalValue;
}

}

【问题讨论】:

    标签: java binary hex octal


    【解决方案1】:

    试试这个代码:

       // Main.java
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {   
        String num = "1110";
    
        int dec = Integer.parseInt(num,2);
    
        String oct = Integer.toOctalString(dec);
    
        System.out.println("Binary " + num + " in Octal radix is "+ oct );
    
      }
    }
    

    【讨论】:

      【解决方案2】:

      你在

      public String binaryToOctal(int decimalValue){
          decimalValue = Integer.parseInt("" + num, 8);
          return "" + decimalValue;
      }
      
      • 忽略参数
      • 解析num,始终保持为0

      所以你总是会得到 0。变化:

      for(int count = 1; count < octConv.length; count++){
          octConv[count] = myOctal.binaryToOctal(count);   // <-- pass in count
      }
      
      public String binaryToOctal(int decimalValue){
          return String.format( "%o", decimalValue );
      }
      

      我承认,使用格式化打印进行基本转换并不是什么大不了的事。当从二进制 (int) 值转换为在某个基数中表示该值的字符串值时,应该对所有基数 2、8、10、16 使用一种算法。

      稍后

      这是我所指的算法,适用于 16 以内的任何基数。

      private static String digits = "0123456789ABCDEF";
      public static String convert( int num, int base ){
          if( num == 0 ) return "0";
          StringBuilder sb = new StringBuilder();
          while( num > 0 ){
              sb.insert( 0, digits.charAt( num % base ) );
              num /= base;
          }
          return sb.toString();
      }
      
      public static void main( String[] args ) throws Exception {
          for( int i = 0; i < 100; i++ ){
              System.out.println( i + " " + 
                                 convert( i, 2 ) + " " +
                                 convert( i, 8 ) + " " +
                                 convert( i, 10 ) + " " +
                  convert( i, 16 ) );
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-21
        • 2014-03-11
        • 1970-01-01
        • 2012-06-25
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        相关资源
        最近更新 更多