十进制到八进制转换 [英] Decimal to Octal Conversion

查看:137
本文介绍了十进制到八进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

十进制转换错误

我正在为一个班级编写程序并且正在编写程序麻烦如何将八进制数转换为十进制数。以下是我到目前为止所做的工作:

I am writing a program for a class and is having trouble figuring how to convert an octal number to an decimal number. Here is what I been trying to do so far:

   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的新手,所以更基本的技术更好,谢谢

so basically I need a way to reduced a number to single digits (ie. 1337 into 1,3,3,7). I would really like to do it with what I have now, but my way of doing seems to have some errors that I can't see. It actually works if I enter a number less then 100, but When I enter a number higher then 100 the conversion is gets messed up somewhere. I am new to java so the more basic the techique the better, thanks

推荐答案

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

The following converts from decimal to octal,

import java.util.Scanner;

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();
        String result= Integer.toString(oct,8);
        System.out.println(result);
    }
}

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

The following converts from octal to decimal,

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);
 }

以下是从八进制转换为十进制的更算法方式

The following is a more algorithmic way of converting from octal to decimal,

     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;
        }
     }

以下是将小数转换为八进制,

The following is for converting a decimal to octal,

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;
}

这篇关于十进制到八进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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