我对java和学习OOP相对较新,我有一个项目可以创建一个数字转换器,可以将任何基数的值转换为十进制,或将十进制值转换为任何基数。
我已经测试过十进制到一个不同的基数,这对我来说很好,但基于[2,8,无论如何]的小数不起作用。有什么帮助吗?
public class NumberConverter
{
private int decimal; // always stores the decimal equivalent, regardless of base
private int base;
private String strValue;
/** default set to base 10 w/ a value of 0 */
public NumberConverter()
{
decimal = 0;
base = 10;
strValue="0";
}
/** base 10 value is used to set decimal, base and strValue
* note: toBaseX can do this for you also */
public NumberConverter(int value)
{
decimal = value;
base = 10;
strValue = "" + value;
}
/** sets the strValue and base based on parameters
* strValue is only stored in uppercase
* decimal is set here as well provided newValue is valid */
public NumberConverter(int newValue, int newBase)
{
decimal = 0;
strValue = newValue + "";
base = newBase;
}
//**** Accessor Methods ****//
public String getValue()
{
return strValue;
}
public int getBase()
{
return base;
}
public int getDecimal()
{
return decimal;
}
/** sets the strValue and base based on parameters
* decimal is set here as well, with a call to baseXToDec() */
public void setValue(String newValue, int newBase)
{
strValue = newValue;
base = newBase;
if (isValid())
decimal = baseXToDec();
else
decimal = 0;
}
public boolean isValid()
{
boolean valid = true;
for (int i=0; i<strValue.length()-1; i++)
{
if ((strValue.charAt(i)>47 && strValue.charAt(i)<58) || (strValue.charAt(i)>64 && strValue.charAt(i)<71))
valid = true;
else
valid = false;
}
return valid;
}
/** base of this object is set to x and strValue is the String value in base x
* strValue is also returned, just for good measure
* @param x the number base to convert to */
public String toBaseX(int x)
{
String strResult = "", strRev;
base = x;
int div = decimal, mod;
if (decimal == 0)
return "0";
while(div != 0)
{
mod = div % x;
if (mod > 10)
strResult += (char)(mod+55);
else
strResult += (char)(mod+48);
div = div / x;
}
strRev = reverseString(strResult);
strValue = strRev;
return strRev;
}
/** take the currently stored strValue and calculate and return the decimal value */
public int baseXToDec()
{
int exponent = strValue.length()-1;
for (int i=0; i<strValue.length(); i++)
{
// update result
decimal += (strValue.charAt(i) * Math.pow(base, exponent));
// decrement exponent
exponent--;
}
base = 10;
strValue = "" + decimal;
return decimal;
}
/** this is a helper method only
* the integer value of digit is returned
* -1 is the return value for an error
* @param ch a valid digit for the given number base */
private int charToValue(char ch)
{
}
/** This is a helper method that returns strRev as a reversed version
* @param strFwd the string to be reversed */
private String reverseString(String strResult)
{
String strRev = "";
for ( int i = strResult.length() - 1 ; i >= 0 ; i-- )
strRev= strRev + strResult.charAt(i);
return strRev;
}
/** a String with the current base and value is returned */
public String toString()
{
String result = "In base " + getBase() + " the value is "+ getValue() + ".\n";
return result;
}
}
答案 0 :(得分:0)
我相信你正试图在这里重新发明轮子。 Java已经很容易实现了。
Integer.toString(number,base)方法将帮助您转换为另一个基础。
示例:
System.out.println(Integer.toString(10, 5));
在需要的地方也可以查看Integer.parseInt()方法。
答案 1 :(得分:0)
strValue.charAt(i)
这就是问题:它为数字返回字符代码,而不是它所代表的整数值。例如,字符'0'的代码是48等。您需要将它转换为实际的整数值,然后才能使用它。
一个(非便携式,不赞成的)方式是减去48(或'0'),如评论中所建议的那样。这取决于ASCII中的数字是按顺序编码的事实,因此49将是'1',50 - '2'等。它会做你想要的。
(稍微)更好的方法是Character.getNumericValue(strValue.charAt(i))
。
它将在幕后做同样的事情,但也将使用异国语言(如印度语,复活节阿拉伯语等),使用不同的数字符号。并不是说你的小程序将需要它,这只是采用未来的好方法。
编辑:同样适用于isValid()函数。 Character.isDigit()是一个更好的选择,明确地查看字符代码。您还可以通过用strValue.matches("\\d+");
替换整个事物来简化它(\\d
是“digit”的正则表达式代码,因此当您的字符串仅包含数字时,这将返回true。
答案 2 :(得分:0)
我还没有到过isValid;如建议的那样减去0的值并使用单独的局部变量确实给了我正确的输出。
public int baseXToDec()
{
int decVal = 0, exponent = strValue.length()-1;
for (int i=0; i<strValue.length(); i++)
{
// update result
decVal += ((strValue.charAt(i)-'0') * Math.pow(base, exponent));
// decrement exponent
exponent--;
}
base = 10;
strValue = "" + decVal;
decimal = decVal;
return decimal;
}
答案 3 :(得分:0)
如果最后一个char有效,则您的方法isValid包含true;如果最后一个char无效,则包含false。 改变它:
public boolean isValid()
{
for (int i=0; i<strValue.length()-1; i++)
{
if (!((strValue.charAt(i)>47 && strValue.charAt(i)<58) || (strValue.charAt(i)>64 && strValue.charAt(i)<71)))
return false;
}
return true;
}