【发布时间】:2014-09-18 07:58:51
【问题描述】:
当我将数字转换为不同类型时,我正在制作这个程序来帮助我。我有一个数组,它显示 1 到 256 之间的每个数字的所有二进制值,以及一个对所有十六进制值执行相同操作的数组。我遇到的问题是显示八进制值。
当我第一次编写这个程序时,我手动输入了每个数字,然后计算全部在程序中完成并显示转换。现在我决定我只想显示从 1 到 256 的每个数字及其对应的二进制、十六进制和八进制值的表格。
显示二进制和十六进制的值没有问题,但是当我进入八进制时,如果我不使用用户输入,所有内容都会显示为零。添加用户输入后,八进制数组的每个值都与我输入的值相同。
我只需要有人查看我的代码并告诉我我的逻辑哪里出错了。
import java.util.*;
public class NumberConverter
{
static int num = 0;
static String bin = "" + num, hex, oct;
public static void main (String [] args)
{
/**
* Allows for user input to enter a number between 1 & 256 which is then passed to three methods for conversion into binary, hexadecimal, and octal values
*/
// Scanner scan = new Scanner (System.in);
// System.out.print("Please enter a positive integer between 1 & 256: "); //prompts the user for a positive integer between 1 & 256
// num = scan.nextInt();
//
// if(num <= 0 || num > 256)
// {
// System.out.println("Invalid number.\n");
// //if the number entered by the user is 0 or less, or greater than 256, an Invalid message is displayed.
// }
//
// else
// {
// System.out.print("\nDecimal to Binary Conversion: ");
// NumberConverter myBinary = new NumberConverter(); //creates a new object called myBinary from the NumberConverter Class
// System.out.println(myBinary.toBinary(num)); //takes the myBinary object and modifies it using the toBinary method
//
// System.out.print("Binary to Hexadecimal Conversion: ");
// NumberConverter myHex = new NumberConverter(); //creates a new object called myHex from the NumberConverter Class
// System.out.println(myHex.binaryToHex("" + num)); //takes the myHex object and modifies it using the binaryToHex method
//
// System.out.print("Binary to Octal Conversion: ");
// NumberConverter myOctal = new NumberConverter(); //creates a new object called myOctal from the NumberConverter Class
// System.out.println(myOctal.binaryToOctal(num)); //takes the myOctal object and modifies it using the binaryToOctal method
// }
/**
* Creates an array to store and display all hexadecimal values for the numbers 1 through 256
*/
String [] hexConv = new String [257];
NumberConverter myHex = new NumberConverter();
for(int count = 1; count < hexConv.length; count++)
{
hexConv[count] = myHex.binaryToHex("" + count);
}
/**
* Creates an array to store and display all Octal values for the binary values of the numbers 1 through 256
*/
String [] octConv = new String [257];
NumberConverter myOctal = new NumberConverter();
for(int count = 1; count < octConv.length; count++)
{
octConv[count] = myOctal.binaryToOctal(num);
}
/**
* Creates an array to store and display all binary values for the numbers 1 through 256
* and prints the contents of all three arrays
*/
String [] binConv = new String [257];
NumberConverter myBinary = new NumberConverter();
System.out.println("\n\n#\tBinary\t\t Hexadecimal\t Octal");
for(int count = 1; count < binConv.length; count++)
{
System.out.print(count + ":\t");
binConv[count] = myBinary.toBinary(count);
System.out.println("\t\t\t" + hexConv[count] + "\t\t" + octConv[count]);
System.out.println("--------------------------------------------------------------");
}
}
public String toBinary (int num)
{
String remainder = "" + num % 2;
if (num < 1)
{
return "" + num;
}
bin = remainder; //remainder value is stored as the value of bin
toBinary(num >> 1);
System.out.print(remainder);
return " ";
}
public String binaryToHex(String binaryValue)
{
String result = "";
int n = Integer.parseInt(binaryValue);
int remainder = n % 16;
if (n == 0)
{
return "";
}
else
{
switch (remainder)
{
case 10:
result = "A";
break;
case 11:
result = "B";
break;
case 12:
result = "C";
break;
case 13:
result = "D";
break;
case 14:
result = "E";
break;
case 15:
result = "F";
break;
default:
result = remainder + result;
break;
}
hex = binaryToHex(Integer.toString(n / 16)) + result;
return hex;
}
}
public String binaryToOctal(int decimalValue)
{
decimalValue = Integer.parseInt("" + num, 8);
return "" + decimalValue;
}
}
【问题讨论】: