Java代码小问题

7aY 2017-11-21 09:21:47
public static String base2base(String n, int b1, int b2) {
// Declare variables: decimal value of n,
// character of base b1, character of base b2,
// and the string that will be returned.
int decimalValue = 0,charB2;
char charB1;
String output="";
// Go through every character of n
for (int i=0; i<n.length(); i++) {
// store the character in charB1
charB1 = n.charAt(i);
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
if (charB1 >= 'A' && charB1 <= 'Z')
charB2 = 10 + (charB1 - 'A');
// Else, store the integer value in charB2
else
charB2 = charB1 - '0';
// Convert the digit to decimal and add it to the
// decimalValue of n
decimalValue = decimalValue * b1 + charB2;
}

// Converting the decimal value to base b2:
// A number is converted from decimal to another base
// by continuously dividing by the base and recording
// the remainder until the quotient is zero. The number in the
// new base is the remainders, with the last remainder
// being the left-most digit.

// While the quotient is NOT zero:
while (decimalValue != 0) {
// If the remainder is a digit < 10, simply add it to
// the left side of the new number.
if (decimalValue % b2 < 10)
output = Integer.toString(decimalValue % b2) + output;
// If the remainder is >= 10, add a character with the
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
else
output = (char)((decimalValue % b2)+55) + output;
// Divide by the new base again
decimalValue /= b2;
}
return output;
}


这段代码中的 int decimalValue = 0,charB2; 为什么 int decimalValue = 0,charB2; 要加个逗号啊,和 int decimalValue = 0;有什么区别...第一次见这种语法 在github上看到的
...全文
183 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Clumsy__Cat 2017-11-22
  • 打赏
  • 举报
回复
就是一次进行多个变量初始化而已
7aY 2017-11-21
  • 打赏
  • 举报
回复
谢谢各位了啊
爱写Bug的麦洛 2017-11-21
  • 打赏
  • 举报
回复
public class IntDemo {
	 
	public static void main(String[] args) {
		int decimalValue = 0,charB2 = 0;
		System.out.println(decimalValue);
		System.out.println(charB2);
	}
}
我刚才测试了一下,这样的意思就是定义两个变量,只不过charB2没有赋初始值, 这种写法应该是一次性定义多个同类型的变量;
jiangyu_java 2017-11-21
  • 打赏
  • 举报
回复
int decimalValue = 0,charB2;相当于 int decimalValue = 0; int charB2;
李德胜1995 2017-11-21
  • 打赏
  • 举报
回复
有什么问题吗???有没有初始化值的区别而已。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧