Java 将以10为基数的数字转换为N
这段代码是我必须开发的项目的一部分。我需要编写一个java方法,将Java 将以10为基数的数字转换为N,java,Java,这段代码是我必须开发的项目的一部分。我需要编写一个java方法,将nbase中的一个数字(以10为基数)转换为另一个数字。这是该类的代码: public class converter { public int from_10_to_n(int base, int newbase) { int k = 0; String res = ""; String output = ""; do {
n
base中的一个数字(以10为基数)转换为另一个数字。这是该类的代码:
public class converter {
public int from_10_to_n(int base, int newbase) {
int k = 0;
String res = "";
String output = "";
do { //Below I explain what I'm doing here
base /= newbase;
k = base % newbase;
res += String.valueOf(k);
} while (base != 0);
for(int i=res.length()-1; i>-1; i--) {
output += res.charAt(i);
}
return Integer.parseInt(output);
}
我想用这种方式制作这个节目:
do{}while()代码>循环将数字分开,并将余数保存在k中。然后我做一个for循环,它反转字符串res(它有提醒)
顺便说一下,当我在main中调用该方法时,我缺少了最后一个数字。我的意思是:
converter k = new converter();
int e = k.from_10_to_n(a /*base 10 number*/, b /*Base n number*/);
System.out.println(a+" -> "+b+" is " + e);
使用此代码,如果要将231转换为基数4,则结果是321
,而不是3213
。我已经检查了代码,但找不到解决方案。有什么想法吗
我在其他基础上也有同样的错误。例如,31(基数10)是11111
(基数2),但我的程序返回1111
翻转循环中前两行的顺序;先除法,就失去了第一个余数。但接下来您需要处理最后的余数。问题在于:
base /= newbase;
k = base % newbase;
用一些实数试试这个,比如例子中的231和基数4
base /= newbase
现在base
是57,您的k
将不正确。您应该先得到余数,然后再除以:
k = base % newbase;
base /= newbase;
<> P>也有一些风格问题,你应该考虑纠正:
base
实际上不包含任何基,只包含输入值-可能将其重命名为input
或类似的名称
- 混合压痕。循环以一个空格缩进,而代码的其余部分有四个空格
我们的十进制字母表是“0123456789”。
现在,任意字母数字系统的字母表以N为基数,大,为
“#0#1#2#9#10#11#1234#N”
因此,总结、简化、增加输出的易读性并进行测试,我们得到:
/*
* Program FromBase10ToBaseN
* This program receives as input
* a positive integer number in base 10
* and outputs its encoding in arbitrary base N.
* The k-th symbol of the new base is denoted #k.
* Tests are provided.
* Jose (2016)
* evoljava.com
*/
package Programs;
import java.util.Random;
public class FromBase10ToBaseN {
private static final Random RANDOM = new Random();
public static void main(String[] args) throws Exception {
//Number in base 10
int inputValue = 9;
//New base
int N = 10;
reportFromBase10ToBaseN(inputValue, N);
inputValue = 100;
N = 10;
reportFromBase10ToBaseN(inputValue, N);
inputValue = 8;
//New base
N = 2;
reportFromBase10ToBaseN(inputValue, N);
inputValue = 129;
//New base
N = 128;
reportFromBase10ToBaseN(inputValue, N);
inputValue = 127;
//New base
N = 128;
reportFromBase10ToBaseN(inputValue, N);
//test with random numbers
for (int i = 0; i < 10; i++) {
inputValue = RANDOM.nextInt(1000);
//New base must be 2 or greater
N = 2 + RANDOM.nextInt(80);
reportFromBase10ToBaseN(inputValue, N);
}
}
private static void reportFromBase10ToBaseN(int inputValue, int N) {
String outputValue = fromBase10ToBaseN(inputValue, N);
int Backwards = fromBaseNToBase10(outputValue, N);
String isRight = "wrong";
if (inputValue == Backwards) isRight = "right";
System.out.println(inputValue + " in base 10 becomes "
+ outputValue
+ " in base " + N
+ " Backwards: " + Backwards
+ " " + isRight);
}
private static String fromBase10ToBaseN(int inputValue, int N) {
String res ;
String outputValue = "";
do {
res = "#" + String.valueOf(inputValue % N);
outputValue = res + outputValue;
inputValue /= N;
} while (inputValue != 0);
return outputValue;
}
//The input string denotes a number in base newBase
//The k-th symbol of the new base is denoted #k.
//Example: #10#0 in base 15 represents 150 in base 10
private static int fromBaseNToBase10(String inputValue, int N) {
if (inputValue.charAt(0) == '#') {
int outputValue = 0;
boolean correct = true;
int length = inputValue.length();
String token = "";
int power = 0;
//Tokenizing:
//Exmpl: #1#120#13#2 is separated into tokens 1 120 13 2
for (int i = length - 1; (i > -1) & correct; i--) {
char c = inputValue.charAt(i);
if (c != '#') {
token = c + token;
} else {
//tokens are evaluated
int p = Integer.parseInt(token);
//Detecting the presence of an alien symbol
if (p >= 0) {
outputValue
= (int) (outputValue + p * Math.pow(N, power));
power++;
} else {
outputValue = -1;
correct = false;
}
token = "";
}
}
return outputValue;
} else return -1;
}
}//End of Program FromBase10ToBaseN
OUTPUT:
9 in base 10 becomes #9 in base 10 Backwards: 9 right
100 in base 10 becomes #1#0#0 in base 10 Backwards: 100 right
8 in base 10 becomes #1#0#0#0 in base 2 Backwards: 8 right
129 in base 10 becomes #1#1 in base 128 Backwards: 129 right
127 in base 10 becomes #127 in base 128 Backwards: 127 right
382 in base 10 becomes #6#40 in base 57 Backwards: 382 right
390 in base 10 becomes #11#5 in base 35 Backwards: 390 right
788 in base 10 becomes #3#1#4 in base 16 Backwards: 788 right
285 in base 10 becomes #3#4#6 in base 9 Backwards: 285 right
68 in base 10 becomes #1#31 in base 37 Backwards: 68 right
656 in base 10 becomes #24#8 in base 27 Backwards: 656 right
442 in base 10 becomes #9#28 in base 46 Backwards: 442 right
765 in base 10 becomes #21#30 in base 35 Backwards: 765 right
455 in base 10 becomes #10#35 in base 42 Backwards: 455 right
211 in base 10 becomes #4#3 in base 52 Backwards: 211 right
/*
*来自Base10Tobasen的程序
*该程序接收作为输入的数据
*以10为基数的正整数
*并以任意基数N输出其编码。
*新基的第k个符号表示为#k。
*提供了测试。
*何塞(2016)
*evoljava.com
*/
一揽子计划;
导入java.util.Random;
来自Base10Tobasen的公共类{
私有静态最终随机=新随机();
公共静态void main(字符串[]args)引发异常{
//以10为基数的数字
int inputValue=9;
//新基地
int N=10;
来自Base10ToBasen的报告(输入值,N);
输入值=100;
N=10;
来自Base10ToBasen的报告(输入值,N);
输入值=8;
//新基地
N=2;
来自Base10ToBasen的报告(输入值,N);
输入值=129;
//新基地
N=128;
来自Base10ToBasen的报告(输入值,N);
输入值=127;
//新基地
N=128;
来自Base10ToBasen的报告(输入值,N);
//随机数检验
对于(int i=0;i<10;i++){
inputValue=RANDOM.nextInt(1000);
//新基数必须为2或更大
N=2+RANDOM.nextInt(80);
来自Base10ToBasen的报告(输入值,N);
}
}
私有静态无效报告FromBase10ToBasen(int-inputValue,int-N){
字符串outputValue=fromBase10ToBaseN(inputValue,N);
向后int=fromBaseNToBase10(输出值,N);
字符串isRight=“错误”;
如果(inputValue==Backwards)isRight=“right”;
System.out.println(以10为基数的inputValue+“变为”
+输出值
+“内底”+N
+“向后:”+向后
+“+isRight”;
}
Base10ToBasen的私有静态字符串(int-inputValue,int-N){
字符串res;
字符串outputValue=“”;
做{
res=“#”+String.valueOf(inputValue%N);
outputValue=res+outputValue;
输入值/=N;
}while(inputValue!=0);
返回输出值;
}
//输入字符串表示base NEBASE中的一个数字
//新基的第k个符号表示为#k。
//示例:#10#基数15中的0表示基数10中的150
Basentobase10中的私有静态int(字符串inputValue,int N){
if(inputValue.charAt(0)='#'){
int outputValue=0;
布尔正确=真;
int length=inputValue.length();
字符串标记=”;
整数幂=0;
//标记化:
//例如:#1#120#13#2被分为令牌1 120 13 2
对于(int i=长度-1;(i>-1)&correct;i--){
字符c=输入值。字符(i);
如果(c!='#'){
令牌=c+令牌;
}否则{
//对令牌进行评估
int p=Integer.parseInt(令牌);
//检测外来符号的存在
如果(p>=0){
输出值
=(int)(输出值+p*数学功率(N,功率));
power++;
}否则{
outputValue=-1;
正确=错误;
}
令牌=”;
}
}
返回输出值;
}否则返回-1;
}
}//从Base10到Tobasen的程序结束
输出:
10垒中的9向后变为#10垒中的9:9右
10垒中的100变为10垒中的#1#0#0向后:100右
第10垒的8变成第2垒的#1#0#0#0向后:8右
第10垒的129向后变成第128垒的#1#1:129右
第10垒的127变为第128垒的127向后:127向右
10垒382变为57垒6#40向后:382右
第10垒的390向后变为第35垒的#11#5:右侧390
第10垒的788向后变成第16垒的#3#1#4:788对
10号基地的285号在9号基地的后方变成3号基地的4号基地的6号基地