在Java中进行十进制转二进制、八进制、十六进制的转换

4

我正在学习面向对象编程,但我不知道该如何做。我只了解基本原理,却不知道怎么做。老师要求我们创建一个程序,将数字从10进制转换成2进制、8进制和16进制,然后再将其从2进制、8进制和16进制转回10进制。我从其他网站上收集了一些信息,并进行了一些编辑。

请帮忙!

我希望你们能够亲自制作并发送给我,但如果这样做太难了,请尽力指导我,因为我对Java不是很熟悉。到目前为止,我的代码如下:

import java.util.Scanner;

public class baseconverterr
{

public static void main(String[] args) {

      // Read the conversion choice from the user

      System.out.println("Choose 1 or 2 or 3:");

      System.out.println("1: conversion from base 10 to base 2 ");

      System.out.println("2: conversion from base 10 to base 8");

      System.out.println("3: conversion from base 10 to base 16");
      // do you want 1, 2 , or 3? you have your choice
      Scanner in = new Scanner(System.in);

      int choice = in.nextInt();

      String string = in.nextLine();

      // Read in the number to be converted and do the conversion

      String output= "";

      System.out.println("Please enter the number to be converted:");

      int input = in.nextInt();

      if (choice == 1)
      // if the user chooses choice #1, it will convert from base 10 to base 2
          output = Integer.toString(input, 2);

      else if (choice == 2)

          output = Integer.toString(input, 8);

    // if the user chooses choice #2, it will convert from base 10 to base of 8 
     else if (choice == 3)

          output = Integer.toString(input, 16);
   // if the user chooses choice #3, it will convert from base 10 to base 16
  else

      System.out.println("invalid entry");
      // everything else, it is invalid
      System.out.println("final output=" + output);
      // this prints the final output.

有一个不错的技巧,从右到左工作,将数字除以新基数的余数将成为下一个数字,每次都将原始数字除以新基数。当您不想为每个新基数编写新函数时,这非常方便。 - chris
2个回答

32

对于十进制数x(使用基数10),可以分别使用以下方式进行二进制、八进制和十六进制转换:

Integer.toString(x, 2)

Integer.toString(x, 8)

Integer.toString(x, 16)

然后,分别从二进制、八进制和十六进制进行还原,返回十进制值:

Integer.valueOf(binary_value, 2)

Integer.valueOf(octal_value, 8)

Integer.valueOf(hex_value, 16)

在你的代码中,请修改以下内容:

output = Integer.toString(input, 16) //replace 16 for hex, 8 for octal, 2 for binary 

1
我需要改变什么?我不明白。我对这完全是个新手... - Justin Park
我解决了。我尝试运行程序,但在选择数字(例如:1,即十进制转二进制转换器)后,当我输入要转换的数字时,它告诉我无效(由我的代码),而不是错误。 - Justin Park
我应该把它放在哪里? - Justin Park
程序已更新至顶部。 - Justin Park
3
他因为本来应该亲自编写算法以便学到些东西,结果没能过关。 - Stijn de Witt
显示剩余6条评论

0

使用这个

    if (choice == 1)

          output = Integer.toString(input, 2);

      else if (choice == 2)

          output = Integer.toString(input, 8);

    // if the user chooses choice #2, it will convert from base 10 to base of 8 
     else if (choice == 3)

          output = Integer.toString(input, 16);

  else

      System.out.println("invalid entry");

我应该把这个放在哪里?是在哪个类或方法里? 更新:我明白了! 但它似乎并没有起作用... - Justin Park
请删除以下代码:System.out.println("请输入要转换的数字:");并添加以下代码: - NPKR
现在,i是“错误的”,我该怎么做才能停止这个错误? - Justin Park
检查最新的代码。 - NPKR
我解决了。我尝试运行程序,但在选择数字(例如:1,即十进制转二进制转换器)后,当我输入要转换的数字时,它告诉我无效(由我的代码),而不是错误。 - Justin Park
于是他第一次尝试了开发世界。是的-是时候去谷歌搜索了。是时候去搜索其他东西了。DYWMTGTFY(意为“你想让我帮你谷歌一下吗?”)。这句话的意思是,实际上,你应该先自己去谷歌搜索一下。 - Rodney P. Barbati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接