- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了以下代码来计算将以 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/
正如标题所说,我需要制作一个函数,在二进制补码中的 2 个碱基、DEC 和 HEX 之间进行转换。该值使用的位数从一开始就已知。 在深入研究之后,我发现了以下算法: 给定一个 DEC 中的数字。 获取
我的用户文档具有以下格式: { userId: "", userAttributes: [ "", "", ... ""
根据这个: Selectivity is the value between 0 and 1, and it is the fraction of rows returned after applyi
这个词有它 FillChar 是用相同值的字节填充内存补丁的最快方法(不是零,因为有 ZeroMemory),但是是否有等效于用相同的序列填充内存(四字节)整数或基数?像 FillInt 或 Fill
我正在努力寻找建模 1 : 0,1 关系的最佳方法(“可能有一个”或“最多有一个”)。我相信这被称为 Z 基数。 例如,假设我有两个类 Widget和 WidgetTest .并非所有 Widget
我使用parseInt找到了一个片段;它用于获取窗口高度。 这是代码: parseInt($(window).height(), 20); 我很困惑为什么使用 20 作为第二个参数。为什么不是 10
要将十进制数转换为基数 2,我使用: int base2 = 10; Convert.ToString(base2, 2); 输出:1010 但是我怎么能做相反的事情呢?即: 输入:1010输出:10
这是一张真实 table 的再现。假设我有这段代码: CREATE TABLE `testTable` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
由于十六进制(基数 16)使用 0-9A-F,并且(我在这里假设)基数 17 使用 0-9A-G,依此类推。什么符号用过一次0-9A-Z都用完了。 最佳答案 你的问题没有标准答案。 “Base 36”
我正在寻找支持 radix 的浏览器列表Number.toString() 中的参数在 JavaScript 中。全部执行toString ,但我找不到他们是否都支持 radix toString 的
这个问题已经有答案了: What is the radix parameter in Java, and how does it work? (6 个回答) 已关闭 5 年前。 public clas
为什么 (73).toString(36) 返回 21 而 (0.73).toString(36) 返回 0。 qa2voha2volfpsnhmyhqia4i 而不是 0.21? 最佳答案 这是因为
我目前正在研究数据库,我看到 degree 和 cardinality 用作相同的术语,或在某些其他学位定义为否。关系中涉及的实体的数量,并进一步分类为一元、二元和三元。 某些放置度数定义为关系类型的
UML(统一建模语言)中的运算符*和运算符0..*有什么区别? 我看到了这两个基数运算符,但是现在我不必使用哪个基数运算符了。 最佳答案 符号“*”是“0 .. *”的快捷方式。在这种情况下使用的正确
我有位于目录“someApp”中的 Angular 应用程序。网址是 http://example-domain/someApp/#/对于一些带有路径的状态 url 是:http://example-
我想一劳永逸地知道如何编写 UML 基数,因为我经常不得不讨论它们(因此非常欢迎证据和来源:) 如果我想解释一下 Mother可以有几个Child任但是 Child有一个而且只有一个 Mother ,
进行字符算术时,规则是以 10 为基数还是以 8 为基数进行计算?我的书上说'A' = 101(基数为8)或65(基数为10),但是当我将基数为8的字符值插入到我的书给出的关于说明这一点的示例中时,我
该程序是将 4 进制数转换为 2 进制数,并且应该就地完成 #include #include void shiftr(char num[],int i) { memmove(num+i,n
这个问题已经有答案了: JavaScript parseInt is giving me wrong number, what I'm doing wrong? [duplicate] (1 个回答)
我遇到了一个小错误,它似乎表明当您传入图像数据作为其源时,在图像完全加载之前调用了 onload 函数。 这是 HTML 这是 JavaScript: var can
我是一名优秀的程序员,十分优秀!