gpt4 book ai didi

java - 求以 14 为基数给出的两个数字的乘积和和,并以 14 为基数回复

转载 作者:行者123 更新时间:2023-12-02 11:41:36 25 4
gpt4 key购买 nike

我编写了以下代码来计算将以 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());

}
}
}

最佳答案

看起来您可能正在尝试重新实现已经存在的功能;从指定基数从 String 转换为 int

Integer 类包含两个对此非常有帮助的方法:

Integer.valueOf(String s, int radix)将接受一个字符串作为输入并尝试将其转换为相应的整数,其中基数是数字的基数。 Integer.valueOf("D", 14) 将返回 13。但是,如果 String 不包含可解析的 int,则该方法将抛出 NumberFormatException

Integer.toString(int i, int raxis)将采用 int 作为输入并返回指定基数的字符串表示形式。

您可以相信,如果输入值不正确,Integer.valueOf(...) 将抛出错误,重新提示用户输入。此外,算术是为整数定义的,与基数无关。

关于java - 求以 14 为基数给出的两个数字的乘积和和,并以 14 为基数回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com