【问题标题】:(Java)Having trouble converting to Hexadecimal(Java)无法转换为十六进制
【发布时间】:2016-02-22 04:52:42
【问题描述】:

我的任务是:编写一个应用程序,打印一个十进制数的二进制、八进制和十六进制等值表 在 1 到 256 的范围内。如果您不熟悉这些数字系统,请在线研究所需信息 将结果放在 JOptionPane.showMesaageDialog 中。

我目前的代码是:

import javax.swing.*;

public class Prog3_22
{
public static void main (String args[])
{

int a;
int b;
int c;

String billybob = "Decimal "+"\tBinary"+"\tOctal "+"\tHex",binary,oct,hex;

for(int x=1;x<=256;x++)
{
    binary="";oct="";hex="";
    c=x;
    a=x;
    b=x;
    while (c>0)
    {
        int rem= c %2;
        binary=rem+binary;
        c=c/2;
    }//end while
    while (b>0)
    {
        int rem= h%16;
        hex=rem+hex;
        b=b/16;
    }
    while (a>0)
    {
        int rem= a%8;
        oct=rem+oct;
        a=a/8;
    }


billybob+="\n" +x+ "\t"+binary+"\t"+oct+"\t"+hex+"\t";
}//end for



JTextArea outputArea = new JTextArea (10,40);
JScrollPane scroller = new JScrollPane (outputArea);
outputArea.setText(billybob);
JOptionPane.showMessageDialog(null,scroller,null,
    JOptionPane.INFORMATION_MESSAGE);
}
    }

除了显示为 0 的 Hex 列之外,它大部分都有效。我不知道如何修复它,所以任何帮助都会很棒。

谢谢!

【问题讨论】:

标签: java binary hex octal


【解决方案1】:

你有一个对 h%16 的随机引用,据我所知没有分配。我认为您打算将其读取为 b%16。我不确定你的代码是如何编译这个错误的。

通过此修复,当 x = 256 时,我得到 hex = 100。

【讨论】:

  • 还有第二个错误,但还有一些事情要做。
  • 当我做出这个改变时,我在十六进制列下得到了数字,但我不相信输出是正确的。输出:docs.google.com/document/d/…
  • 您的系统是否应该将数字转换为字母?看起来你没有任何东西可以处理。在x = 31的情况下,hex = 1F,根据this converter.
猜你喜欢
  • 1970-01-01
  • 2017-10-06
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
相关资源
最近更新 更多