【问题标题】:Decimal to Octal Conversion [duplicate]十进制到八进制转换[重复]
【发布时间】:2012-10-20 06:31:04
【问题描述】:

可能重复:
Decimal Conversion error

我正在为一个班级编写一个程序,但无法弄清楚如何将八进制数转换为十进制数。到目前为止,这是我一直在尝试做的事情:

   import java.util.Scanner;

public class test   
{ 
public static void main ( String args[])
{
    Scanner input = new Scanner(System.in);

    System.out.print("Enter number: ");
    int oct  = input.nextInt();
    int d2count = 0;
    int result=0;
    int d3count = 0;
    int d3 = 0;
    int d2 = 0;

    d3 = oct;
    d2 = oct;

    if(oct < 1000){
    while ( d3 >= 100){
    d3 = d3 - 100;
        d3count++;
    }}

    if (oct < 100){
    while ( d2 >= 10){
    d2 = d2 - 10;
        d2count++;
    }}

    result = (d3count * 64)+(d2count * 8) + d2;
System.out.printf("%d\n",result);
}
}

所以基本上我需要一种方法将数字减少到个位数(即 1337 到 1、3、3、7)。 我真的很想用我现在拥有的东西来做,但是我的做事方式似乎有一些我看不到的错误。如果我输入一个小于 100 的数字,它实际上可以工作,但是当我输入一个大于 100 的数字时,转换会在某处搞砸。我是java新手,所以技术越基础越好,谢谢

【问题讨论】:

标签: java decimal octal


【解决方案1】:

以下从十进制转换为八进制,

导入 java.util.Scanner;

public class test { 
    public static void main ( String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number: ");
        int oct  = input.nextInt();
        String result= Integer.toString(oct,8);
        System.out.println(result);
    }
}

以下将八进制转换为十进制,

public static void main ( String args[]) {
     Scanner input = new Scanner(System.in);
     System.out.print("Enter number: ");
     String oct  = input.next();
     int result= Integer.parseInt(oct,8);
     System.out.println(result);
 }

下面是八进制转十进制的算法,

     public static int convert(String oct) {
         int i= 0;  
         for(int j = 0; j < oct.length(); j++) { 
                char num = oct.charAt(j);           
                num -= '0';     
                if(num<0||num>7) {          
                    sysout("invalid number");
                    return -1;
                }
                i *= 8;                          
                i += num;                      
            }
            return i;
        }
     }

以下是用于将十进制转换为八进制,

public static int convert(int OctalNumber){
   int counter=0;
   int result = 0;
   while(OctalNumber !=0) {
        int temp = (int) ((OctalNumber%8) * Math.pow(10, counter));
        counter++;
        result += temp;
        OctalNumber /= 8;
    }
    return result;
}

【讨论】:

  • 从他的代码来看,我认为他实际上是在将八进制转换为十进制。
  • 所以,他可能编码错误,因为他说他想将十进制转换为八进制。
  • 他更可能打错了他的标题,因为他说他的代码适用于其他输入。另外,由于这是一个任务,我假设他会寻找一个实际的算法。
  • @Jordan Kaye 更新了我的答案,看看吧。
  • 这很接近了。您需要根据您的索引乘以 8 的适当幂。
【解决方案2】:

【讨论】:

  • 这个不会用,上课还没学过...
  • 所以在课堂上展示你的领先地位:)
【解决方案3】:

首先,您的代码将八进制转换为十进制。

其次,你有

if(oct < 1000){

if (oct < 100){

但是您没有if 语句来处理输入数字大于1000 的情况。

编辑:我刚刚意识到这真的不是问题。当您开始计算 d2 时,您需要在减去 100 多次后使用 d3 的 end 值。所以在第二个之前,如果你需要一个

d2 = d3;

不过,您仍然需要处理大于 1000 的输入。

【讨论】:

    【解决方案4】:

    您可以使用一些更好的算法,允许任何合理长度的十进制数(即不会导致溢出):

    public int octalToDecimal(int octal)
    {         
       int numDigits = Integer.toString(octal).length();
       int decimalResult = 0;
       int mask = 0x00000007;
       for(int i = 0; i < numDigits; i++)
       {
          int octalDigit = octal & mask;
          octal = octal >> 3;    
          decimalResult += octalDigit * Math.pow(8, i);
       }
    
       return decimalResult;
    }
    

    【讨论】:

    • 我认为你的 mask 应该是 7,而不是 0xF。此外,您应该使用 10 的幂,而不是 8 的幂。您应该将 octal 移动 3,而不是 1。
    • 在第一个和第三个帐户上正确,但是为什么要使用 10 的幂来乘以八进制数字?这与将二进制数字乘以 10 的幂是一样的 - 没有意义。
    • 是的,变量名decimalResult 让我失望了,我猜。我的错。
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 2011-08-21
    • 2015-12-12
    • 1970-01-01
    • 2011-02-04
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多