2015-11-19 52 views
0

我的任務是:編寫一個應用程序,該應用程序打印一個二進制,八進制和十六進制等效小數 的表格,範圍爲1到256.如果您不熟悉這些數字系統在線研究所需信息將結果放入JOptionPane.showMesaageDialog中。(Java)在轉換爲十六進制時遇到問題

我的代碼目前是:

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。我不知道如何解決它所以任何幫助將是巨大的。

謝謝!

+0

難道我們看到輸出的樣本? –

+0

不知道如何把這一切,對不起。 https://docs.google.com/document/d/10mARN16lpTORfO2XtuppO-cKLQruJydJ02GCYGeOPVE/edit?usp=sharing – barno

回答

0

你有一個隨機參考h%16,據我所知,這是不分配的。我認爲你打算把它寫成b%16。我不確定你的代碼是如何編譯這個錯誤的。

用此修復程序,我得到十六進制= 100當x = 256

+0

仍然存在第二個錯誤,但仍有一些問題需要解決。 –

+0

當我做這個改變,我得到十六進制列下的數字,但我不相信輸出是正確的。輸出:https://docs.google.com/document/d/1sKIFKRhOBvdY0lu3BHaow8iVLsg7ZsBZEhsrdpF_qVE/edit?usp=sharing – barno

+0

您的系統應該將數字轉換爲字母嗎?看起來你沒有任何處理這件事的地方。在x = 31,hex = 1F的情況下,根據[this converter](http://www.binaryhexconverter.com/decimal-to-hex-converter)。 – Danielle

相關問題