我编写了以下代码来计算将在基数14中输入的两个数字的总和和数。答案也应该在14号基数中。
我没有任何编译错误。但答案是垃圾。任何人都可以帮我找出问题所在?
程序应该提示用户输入两个基于14的数字,然后显示总和和 这两个产品输入了14个数字。产出也应该是基于14的。
import java.util.Scanner;
public class H6_gedion {
public static void main(String[] args) {
double[] Answer;
final int base = 14;
Scanner get = new Scanner(System.in);
String[] input = new String[2];
System.out.println("Welcome to earth!!\nPlease Enter two 14-based
numbers in the next line: ");
input = get.nextLine().trim().split("\\s+"); String num1 = input[0]; String num2 = input[1]; ValidateNumInBase(num1, base); ValidateNumInBase(num2, base);
//Answer = compute(toDecimal(num1, num2));
//System.out.println(Answer[0] + " " + Answer[1]);
toBase14(compute(toDecimal(num1, num2)));
}
public static void ValidateNumInBase(String num, int base) {
char chDigit;
for (int d = 0; d < num.length(); d++) {
chDigit = num.toUpperCase().charAt(d);
if (Character.isDigit(chDigit) && (chDigit - '0') >= base) {
System.out.println("cannot have digit " + chDigit + " in base " +
base);
System.exit(1);
} else if (Character.isLetter(chDigit) && (chDigit - 'A') + 10 >=
base) {
System.out.println("cannot have digit " + chDigit + " in
base " +
base);
System.exit(1);
} else if (!Character.isDigit(chDigit) &&
!Character.isLetter(chDigit)) {
System.out.println("Invalid digit character " + chDigit);
System.exit(1);
}
}
}
public static double[] toDecimal(String num1, String num2) {
double val1 = 0;
double val2 = 0;
double decDigit = 0;
char chDigit;
int L = num1.length();
for (int p = 0; p < L; p++) {
chDigit = Character.toUpperCase(num1.charAt(L - 1 - p));
if (Character.isLetter(chDigit)) {
decDigit = chDigit - 'A' + 10;
} else if (Character.isDigit(chDigit)) {
decDigit = chDigit - '0';
} else {
System.out.println("error: unrecognized digit");
System.exit(1);
}
val1 += decDigit * Math.pow(10, p);
}
L = num2.length();
for (int p = 0; p < L; p++) {
chDigit = Character.toUpperCase(num2.charAt(L - 1 - p));
if (Character.isLetter(chDigit)) {
decDigit = chDigit - 'A' + 10;
} else if (Character.isDigit(chDigit)) {
decDigit = chDigit - '0';
} else {
System.out.println("error: unrecognized digit");
System.exit(1);
}
val2 += decDigit * Math.pow(10, p);
}
double[] decimalNum = {
val1,
val2
};
return decimalNum;
}
public static double[] compute(double[] decimalNum) {
double sum = decimalNum[0] + decimalNum[1];
double prod = decimalNum[0] * decimalNum[1];
double[] Solution = {
sum,
prod
};
return Solution;
}
public static void toBase14(double[] Solution) {
double val = Solution[0];
//detrmine the number of digits in base 14
int D = 1;
for (; Math.pow(14, D) <= val; D++) {}
//use char array to hold the new digits
char[] newNum = new char[D];
double pwr;
for (int p = D - 1; p >= 0; p--) {
pwr = Math.pow(14, p);
double decDigit = Math.floor(val / pwr);
val -= decDigit * pwr;
if (decDigit <= 9) {
newNum[D - 1 - p] = (char)('0' + (int) decDigit);
} else {
newNum[D - 1 - p] = (char)('A' + (int)(decDigit - 10));
}
}
val = Solution[1];
//detrmine the number of digits in base 14
//int D =1;
for (; Math.pow(14, D) <= val; D++) {}
//use char array to hold the new digits
char[] newNum2 = new char[D];
//double pwr;
for (int p = D - 1; p >= 0; p--) {
pwr = Math.pow(14, p);
double decDigit = Math.floor(val / pwr);
val -= decDigit * pwr;
if (decDigit <= 9) {
newNum2[D - 1 - p] = (char)('0' + (int) decDigit);
} else {
newNum2[D - 1 - p] = (char)('A' + (int)(decDigit - 10));
}
}
System.out.println("Sum: " + newNum.toString());
System.out.println("Product: " + newNum2.toString());
}
}
}
答案 0 :(得分:1)
看起来您可能正在尝试重新实现已存在的功能;从指定的基础转换为String
到int
。
Integer
类包含两个对此非常有帮助的方法:
Integer.valueOf(String s, int radix)
将String作为输入并尝试将其转换为相应的整数,其中radix是数字的基数。 Integer.valueOf("D", 14)
将返回13.但是,如果String不包含可解析的int,则该方法将抛出NumberFormatException
。
Integer.toString(int i, int raxis)
将int作为输入并返回指定基数的字符串表示。
如果输入值不正确以重新提示用户输入,则可以依赖Integer.valueOf(...)
将抛出错误的事实。此外,无论基数如何,都为整数定义算术。