有人可以为我解释这段代码吗?基本转换代码
Can someone explain this code for me? Base-Conversion Code
因此,对于家庭作业,我们必须编写一个程序,将数字从一个基数转换为另一个基数(即 2 进制的 110 到 10 进制的 6)。我问我的朋友他是怎么做到的,因为我遇到了麻烦,他只给我发了他的代码,没有别的。有人可以解释这段代码的逻辑,以便我可以编写自己的程序并真正理解如何解决这个问题。谢谢!
import java.util.*;
public class Base_Converter {
public static final String value = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String args[]){
int x, y;
String num, base10 = "";
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number you want to convert.");
num = scan.nextLine();
num = num.toUpperCase();
System.out.println("What base is it in?");
x = scan.nextInt();
System.out.println("What base do you want to convert it to?");
y = scan.nextInt();
if(x <= 36 && y <= 36 && x > 1 && y > 1){
base10 = toBase10(num,x);
num = newBase(base10,y);
System.out.println(num);
}
}
public static String toBase10(String num, int from){
long total = 0;
int counter = num.length();
char[] stringArray = num.toCharArray();
for(char w : stringArray){
counter--;
total += value.indexOf(w)*Math.pow(from,counter);
}
return String.valueOf(total);
}
public static String newBase(String num, int to){
String total = "";
int current = 0;
while(Integer.valueOf(num) > 0){
current = Integer.valueOf(num)%to;
total = value.charAt(current)+total;
num = String.valueOf(Integer.valueOf(num)/to);
}
return total;
}
}
我认为你不应该关注你朋友的代码做了什么,而应该关注你自己如何完成作业,因为我认为你的问题在于你缺乏理解。不过,我不会让您高枕无忧,而是会带您了解基础转换的一些细节。
首先,读取用户输入。看起来您正在使用 Java,因此只需使用 scanner 即可。至少您需要读取要转换的数字、它的基数以及输出的基数。
接下来,我们要转换数字。您可以直接将数字相互转换(即将 2 进制转换为 8 进制),但这需要比我现在愿意提供的更多的脑力。相反,我建议始终先将用户输入的数字转换为以 10 为底数的数字(就像您的朋友所做的那样)。那么我们如何将一些未知的基数转换为基数 10 呢?
那么让我们分解一下数字的表示方式:假设我们有以十为底的数字 234
。这相当于 4*10^0 + 3*10^1 + 2*10^2
或 4 + 30 + 200 = 234
。您可以对任何其他数字使用相同的转换。 IE。如果数字以 8 为基数 1763
,则以 10 为基数的值将是 3*8^0 + 6*8^1 + 7*8^2 + 1*8^3
或 3 + 48 + 448 + 512 = 1011 base 10
(try entering 1763 here for proof. So to convert to decimal, you just need to see to multiply each individual number time your base to the power of its place minus 1. For example, since 1
is the fourth number in the 1763
you multiply it times 8^(4-1)
. Since, you are reading a string from the user. You'll need to convert each character of the string to an integer using the ascii chart。
现在从十进制转换为任何数。不用乘法,您只需除以每个值并写出余数! I'll let someone else describe this procedure.
现在只需将这个新值存储为字符串,执行类似
的操作
String output = "";
output += newValue;
在计算机科学中,仅仅复制别人的代码弊大于利。希望这对您有所帮助!
因此,对于家庭作业,我们必须编写一个程序,将数字从一个基数转换为另一个基数(即 2 进制的 110 到 10 进制的 6)。我问我的朋友他是怎么做到的,因为我遇到了麻烦,他只给我发了他的代码,没有别的。有人可以解释这段代码的逻辑,以便我可以编写自己的程序并真正理解如何解决这个问题。谢谢!
import java.util.*;
public class Base_Converter {
public static final String value = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String args[]){
int x, y;
String num, base10 = "";
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number you want to convert.");
num = scan.nextLine();
num = num.toUpperCase();
System.out.println("What base is it in?");
x = scan.nextInt();
System.out.println("What base do you want to convert it to?");
y = scan.nextInt();
if(x <= 36 && y <= 36 && x > 1 && y > 1){
base10 = toBase10(num,x);
num = newBase(base10,y);
System.out.println(num);
}
}
public static String toBase10(String num, int from){
long total = 0;
int counter = num.length();
char[] stringArray = num.toCharArray();
for(char w : stringArray){
counter--;
total += value.indexOf(w)*Math.pow(from,counter);
}
return String.valueOf(total);
}
public static String newBase(String num, int to){
String total = "";
int current = 0;
while(Integer.valueOf(num) > 0){
current = Integer.valueOf(num)%to;
total = value.charAt(current)+total;
num = String.valueOf(Integer.valueOf(num)/to);
}
return total;
}
}
我认为你不应该关注你朋友的代码做了什么,而应该关注你自己如何完成作业,因为我认为你的问题在于你缺乏理解。不过,我不会让您高枕无忧,而是会带您了解基础转换的一些细节。
首先,读取用户输入。看起来您正在使用 Java,因此只需使用 scanner 即可。至少您需要读取要转换的数字、它的基数以及输出的基数。
接下来,我们要转换数字。您可以直接将数字相互转换(即将 2 进制转换为 8 进制),但这需要比我现在愿意提供的更多的脑力。相反,我建议始终先将用户输入的数字转换为以 10 为底数的数字(就像您的朋友所做的那样)。那么我们如何将一些未知的基数转换为基数 10 呢?
那么让我们分解一下数字的表示方式:假设我们有以十为底的数字 234
。这相当于 4*10^0 + 3*10^1 + 2*10^2
或 4 + 30 + 200 = 234
。您可以对任何其他数字使用相同的转换。 IE。如果数字以 8 为基数 1763
,则以 10 为基数的值将是 3*8^0 + 6*8^1 + 7*8^2 + 1*8^3
或 3 + 48 + 448 + 512 = 1011 base 10
(try entering 1763 here for proof. So to convert to decimal, you just need to see to multiply each individual number time your base to the power of its place minus 1. For example, since 1
is the fourth number in the 1763
you multiply it times 8^(4-1)
. Since, you are reading a string from the user. You'll need to convert each character of the string to an integer using the ascii chart。
现在从十进制转换为任何数。不用乘法,您只需除以每个值并写出余数! I'll let someone else describe this procedure.
现在只需将这个新值存储为字符串,执行类似
的操作String output = "";
output += newValue;
在计算机科学中,仅仅复制别人的代码弊大于利。希望这对您有所帮助!