我怎么能把我的二进制,八进制和十六进制放在一个循环中

时间:2016-05-30 19:20:53

标签: java

所以我本周的目标是找到十六进制的八进制和二进制的十进制。我能够得到十六进制,二进制和八进制,但是在不同的公共类上是单独的循环。所以我想知道如何在一个循环中创建这个代码并读取十六进制,八进制和二进制。

十进制到十六进制

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

    System.out.println("Enter a decimal number: ");
    int decimal = input.nextInt();

    String hex = "";

    while (decimal != 0 ) {
        int hexValue = decimal % 16;

        char hexDigit = (hexValue <= 9 && hexValue > 0) ?
                (char) (hexValue + '0') : (char)(hexValue - 10 + 'A');

                hex = hexDigit + hex;

                decimal = decimal / 16;
        }
    System.out.println("The hex number is " + hex);
    }
}

十进制到八进制

import java.util.Scanner;
public class octal {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a decimal number: ");
        int decimal = input.nextInt();
        String octal = "";

        while ( decimal > 0 ) {
            int remainder = decimal % 8;
            octal = remainder + octal;

            decimal = decimal / 8;
        }
        System.out.println("Octal number:  " + octal);
    }

}

十进制到二进制

 import java.util.Scanner;
    public class GuessNumbers {

        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a decimal number: ");
            int decimal = input.nextInt();
            String binary = "";
            while (decimal > 0) {
                int remainder = decimal % 2;
                binary = remainder + binary;
                decimal = decimal / 2;
            }
            System.out.println("Binary number: " + binary);
        }

    }

2 个答案:

答案 0 :(得分:1)

简单的方法是使用已存在的转换,例如

Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = Integer.toHexString(decimal);
String oct = Integer.toOctalString(decimal);
String bin = Integer.toBinaryString(decimal);

如果您需要整数值而不是字符串,则可以使用

    int h = Integer.parseInt(hex, 16);
    int o = Integer.parseInt(oct, 8);
    int b = Integer.parseInt(bin, 2);

假设您不想使用这些方法(假设您有理由)。

首先,您需要将代码放在方法中,而不是在main中。

然后你可以这样做:

public class Class {

public static void uncode() {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a decimal number: ");
    int decimal = input.nextInt();

    String hex = "";

    while (decimal != 0) {
        int hexValue = decimal % 16;

        char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
                : (char) (hexValue - 10 + 'A');

        hex = hexDigit + hex;

        decimal = decimal / 16;
    }
    System.out.println("The hex number is " + hex);
}

public static void octal() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a decimal number: ");
    int decimal = input.nextInt();
    String octal = "";

    while (decimal > 0) {
        int remainder = decimal % 8;
        octal = remainder + octal;

        decimal = decimal / 8;
    }
    System.out.println("Octal number:  " + octal);
}

public static void GuessNumbers() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a decimal number: ");
    int decimal = input.nextInt();
    String binary = "";
    while (decimal > 0) {
        int remainder = decimal % 2;
        binary = remainder + binary;
        decimal = decimal / 2;
    }
    System.out.println("Binary number: " + binary);
}

public static void allInOne() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a decimal number: ");
    int decimal = input.nextInt();
    int hex = decimal;
    int oct = decimal;
    int bin = decimal;

    String hexal = "";
    String octal = "";
    String binary = "";

    while (hex > 0 || oct > 0 || bin > 0) {
        if (hex > 0) {
            // Get Hexal
            int hexValue = hex % 16;

            char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
                    : (char) (hexValue - 10 + 'A');

            hexal = hexDigit + hexal;

            hex = hex / 16;
        }
        if (oct > 0) {
            // Get Octal
            int remainder = oct % 8;
            octal = remainder + octal;

            oct = oct / 8;
        }
        if (bin > 0) {
            // Get Binary
            int remainder = bin % 2;
            binary = remainder + binary;
            bin = bin / 2;
        }
    }
    System.out.println("The hex number is " + hexal);
    System.out.println("Octal number:  " + octal);
    System.out.println("Binary number: " + binary);
}

public static void main(String[] args) {
    uncode();
    octal();
    GuessNumbers();
    allInOne();
}

}

我尝试尽可能少地修改代码。

答案 1 :(得分:0)

在这里,我通过调用方法getEveryFromDeci(param1,param2)从十进制转换为八进制,二进制,十六进制,其中param1-任何十进制数字,param2-其基值是8,2,16。     我也通过调用方法allToDeci(param1,param2)将八进制,二进制,十六进制转换为十进制,其中param1-字符串形式的十六进制,二进制,八进制值和十六进制的param2-基值

private String getEveryFromDeci(Integer x,Integer y){

    List<String> al = deciBin(x,y,new ArrayList<String>());
    StringBuffer buffer = new StringBuffer();
    for(String s : al)
        buffer.append(s);

    return buffer.toString();

}

private List<String> deciBin(Integer a,Integer b,List<String> list){
    if(a>=b){

        deciBin(a/b,b,list);
        list.add(a%b > 9 ? getHexaDecimal(a%b):Integer.toString(a%b));
    }else
        list.add(Integer.toString(a));

    return list;
}

private String getHexaDecimal(int d){
    String s= null;
    switch(d){
    case 10:
        s="A";
        break;
    case 11:
        s="B";
        break;
    case 12:
        s="C";
        break;
    case 13:
        s="D";
        break;
    case 14:
        s="E";
        break;
    case 15:
        s="F";
        break;
    }

    return s;
}

private int allToDeci(String applyNum,int type){
    int sum =0;
    char[] ch = applyNum.toCharArray();
    for(int pum=0;pum<ch.length;pum++)
        sum += Character.isDigit(ch[pum]) ? getAct(ch.length-(pum+1),type) * Character.getNumericValue(ch[pum]) :getAct(ch.length-(pum+1),type) * getNum(ch[pum]);

    return sum;
}

private int getNum(char ch){
    int num = 0;
    switch(ch){
    case 'A':
        num =10;
        break;
    case 'B':
        num = 11;
        break;
    case 'C':
        num =12;
        break;
    case 'D':
        num =13;
        break;
    case 'E':
        num =14;
        break;
    case 'F':
        num=15;
        break;
        default:
            num =Character.getNumericValue(ch);
            break;
    }

    return num;
}

private int getAct(int k,int p){

    int s=1;
    if(k >0){
        for(int i=0;i<k;i++)
            s *=p;
        return s;
    }else
        return 1;   

}