如何在Java中从任意基转换为基10
我是Java新手。我想写一个程序,只使用算术运算将基数2,3,4,5,6,7,8,9,16转换为基数10 我已经完成了从键盘读取字符串(如果数字是十六进制的话)并将其转换为整数的过程,之后我做了一个while循环,将数字拆分为数字并将其反转 现在我不知道如何使这个数字在0,1,2的幂下乘以2(在二进制情况下)将数字转换为10进制 例如1001(十进制数字9)类似于1x2(功率0)+0x2(功率1)+0x2(功率2)+1x2(功率3) 我的代码:如何在Java中从任意基转换为基10,java,Java,我是Java新手。我想写一个程序,只使用算术运算将基数2,3,4,5,6,7,8,9,16转换为基数10 我已经完成了从键盘读取字符串(如果数字是十六进制的话)并将其转换为整数的过程,之后我做了一个while循环,将数字拆分为数字并将其反转 现在我不知道如何使这个数字在0,1,2的幂下乘以2(在二进制情况下)将数字转换为10进制 例如1001(十进制数字9)类似于1x2(功率0)+0x2(功率1)+0x2(功率2)+1x2(功率3) 我的代码: public static void main(S
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Introduceti din ce baza doriti sa convertiti numarul: 2, 3, 4, 5, 6, 7, 8, 9, 16 ");
int n = Integer.parseInt(br.readLine());
Scanner scanner = new Scanner(System.in);
System.out.println("Introduceti numarul care doriti sa fie convertit din baza aleasa ");
String inputString = scanner.nextLine();
if (n==2){
int conv = Integer.parseInt(inputString);
while (conv>0){
System.out.println (conv%10);
conv = conv/10;
}
}
}
使用:
相反,可通过以下方式实现:
试着这样做:
class Bases
{
public static void main(String[] args)
{
//tests
String l1 = "01010101"; //base 2, 85
String l2 = "123123123"; // base 4, 112347
String l3 = "FFFF"; //base 16, 65535
System.out.println(rebase(l1,2));
System.out.println(rebase(l2,4));
System.out.println(rebase(l3,16));
}
//symbols array
private static final String SYMBOLS = "0123456789ABCDEF";
//actual algorithm
public static long rebase(String number, int base)
{
long result = 0;
int position = number.length(); //we start from the last digit in a String (lowest value)
for (char ch : number.toCharArray())
{
int value = SYMBOLS.indexOf(ch);
result += value * pow(base,--position); //this is your 1x2(pow 0)+0x2(pow 1)+0x2(pow 2)+1x2(pow 3)
}
return result;
}
//power - don't know if this is needed?
private static long pow(int value, int x)
{
if (x == 0) return 1;
return value * pow(value,x-1);
}
}
如果这是你的课堂评估,那么你应该花一些时间来理解代码。如果允许的话,可以用内置Java函数替换pow()函数
public class base2ToBase10Conversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Input Base 2 value:");
int a = input.nextInt();
int b = a/10000000;
double c = b* Math.pow(2, 7);
int d = Math.abs(a-(10000000*b));
int e = d/1000000;
double f = e* Math.pow(2,6);
int g = Math.abs(d-(1000000*e));
int h = g/100000;
double i = h * Math.pow(2,5);
int j = Math.abs(g-(100000*h));
int k = j/10000;
double l = k * Math.pow(2,4);
int m = Math.abs(j-(10000*k));
int n = m/1000;
double o = n * Math.pow(2, 3);
int p = Math.abs(m-(1000*n));
int q = p/100;
double r = q* Math.pow(2, 2);
int s = Math.abs(p-(100*q));
int t = s/10;
double u = t* Math.pow(2,1);
int v = Math.abs(s-(10*t));
double base10 = c + f + i + l + o + r + u + v;
System.out.println("Valuse in Base 10: " + base10);
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();//235
int b=in.nextInt();//8
int result=getBase10(s,b);//getBase10(“235”,8);
系统输出打印项次(结果);
}
私有静态int getBase10(字符串s,int b){
int base=0,pow=0;
int[]a=新的int[s.length()];
对于(int i=0;i=0;i--){
base+=a[i]*Math.pow(b,pow);//转换的通用公式
pow++;
}
System.out.println(“Base 10:+Base);//Base=157
返回基;//157
}
所有IO都与这个问题有什么关系?我不理解你的问题。它非常简单,你可以通过自己尝试解决它来了解更多。您只需要一个循环和一个表达式。您不需要强大的IO,因为这只是较慢/更复杂。Scanner
、BufferedReader
,以及所有其他IO类与您的问题无关。请编辑您的问题,将您的代码缩减到说明您的问题所必需的内容。用户,这无关紧要。@LutzHorn指出的是,代码的这一部分与你的直接问题无关——如何操作数字的基数——只会分散那些试图理解你的代码和问题的人的注意力。如果你把你发布的代码缩减到问题本身和编译所需的最小值,那就更好了。从任意基数到10基数,而不是从二进制基数到其他基数,直到什么?缺少什么?“仅使用算术运算”是正确的,但为什么要重新发明轮子?Java包含Integer
类。所以让我们使用它。当然,如果这个问题是家庭作业,而且某个奇怪的老师禁止使用JDK,那么这个问题在这里是离题的:)如果提出问题的人没有表现出他试图解决问题,只是要求解决问题。这不仅不会像问题所问的那样转换成任何基础,但它甚至不包括有效的基2值,如100000000001
(257)天哪,这是。。。丑陋的。。。为什么是两个循环?为什么是数组?
class Bases
{
public static void main(String[] args)
{
//tests
String l1 = "01010101"; //base 2, 85
String l2 = "123123123"; // base 4, 112347
String l3 = "FFFF"; //base 16, 65535
System.out.println(rebase(l1,2));
System.out.println(rebase(l2,4));
System.out.println(rebase(l3,16));
}
//symbols array
private static final String SYMBOLS = "0123456789ABCDEF";
//actual algorithm
public static long rebase(String number, int base)
{
long result = 0;
int position = number.length(); //we start from the last digit in a String (lowest value)
for (char ch : number.toCharArray())
{
int value = SYMBOLS.indexOf(ch);
result += value * pow(base,--position); //this is your 1x2(pow 0)+0x2(pow 1)+0x2(pow 2)+1x2(pow 3)
}
return result;
}
//power - don't know if this is needed?
private static long pow(int value, int x)
{
if (x == 0) return 1;
return value * pow(value,x-1);
}
}
public class base2ToBase10Conversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Input Base 2 value:");
int a = input.nextInt();
int b = a/10000000;
double c = b* Math.pow(2, 7);
int d = Math.abs(a-(10000000*b));
int e = d/1000000;
double f = e* Math.pow(2,6);
int g = Math.abs(d-(1000000*e));
int h = g/100000;
double i = h * Math.pow(2,5);
int j = Math.abs(g-(100000*h));
int k = j/10000;
double l = k * Math.pow(2,4);
int m = Math.abs(j-(10000*k));
int n = m/1000;
double o = n * Math.pow(2, 3);
int p = Math.abs(m-(1000*n));
int q = p/100;
double r = q* Math.pow(2, 2);
int s = Math.abs(p-(100*q));
int t = s/10;
double u = t* Math.pow(2,1);
int v = Math.abs(s-(10*t));
double base10 = c + f + i + l + o + r + u + v;
System.out.println("Valuse in Base 10: " + base10);
}
}
Consider an example,
Convert (235) base 8 into base 10.
5 x 8^0 = 5
3 x 8^1 = 24
2 x 8^2 = 128
Now simply add these values together.
5 + 24 + 128 = 157
Answer: (235)base 8 = (157)base 10
For more example, refer [This URL][1]
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next(); //235
int b = in.nextInt(); //8
int result = getBase10(s, b); //getBase10("235",8);
System.out.println(result);
}
private static int getBase10(String s, int b) {
int base = 0, pow = 0;
int[] a = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
a[i] = s.charAt(i) - '0'; //Convert into int array
}
for (int i = a.length - 1 ; i >= 0 ; i--) {
base += a[i] * Math.pow(b,pow); //Generalised formula for conversion
pow++;
}
System.out.println("Base 10 : "+base); // base = 157
return base; //157
}