这是我的代码
public class zy14 {
public static void main(String[] args) {
Scanner r=new Scanner(System.in);
int m=r.nextInt();
int n=r.nextInt();
System.out.println("shi jz"+" "+"er jz"+" "+"ba jz"+" "+"sl jz");
for(int i=m;i<=n;i++){
System.out.print(i+" ");
ejz(i);
System.out.print(" ");
bjz(i);
System.out.print(" ");
sljz(i);
System.out.println();
}
r.close();
}
public static void ejz(int i){
Stack<Integer> s=new Stack<>();
int e;
while(i!=0){
s.push(i%2);
i=i/2;
}
while(!s.empty()){
e=s.pop();
System.out.print(e);
}
}
public static void bjz(int i){
Stack<Integer> s=new Stack<>();
int e;
while(i!=0){
s.push(i%8);
i=i/8;
}
while(!s.empty()){
e=s.pop();
System.out.print(e);
}
}
public static void sljz(int i){
Stack<Integer> s=new Stack<>();
int m=0;int e;
while(i!=0){
if ((i%16) <= 9) {
m=i%16;
}
else{
switch (i%16) {
case 10:
System.out.print('A');
break;
case 11:
System.out.print('B');
break;
case 12:
System.out.print('C');
break;
case 13:
System.out.print('D');
break;
case 14:
System.out.print('E');
break;
case 15:
System.out.print('F');
break;
}
}
s.push(m);
i=i/16;
}
while(!s.empty()){
e=s.pop();
System.out.print(e);
}
}
}
要达成的目标:
我的运行结果:
你的问题在于,用了栈来存和取,但是中间10以上的,却直接输出了,顺序就乱了。我下面改成保存每一个字符,按栈的入和出
public static void sljz(int i) {
Stack<Character> s = new Stack<>();
char c;
int m;
char e;
while (i != 0) {
m = i % 16;
switch (m) {
case 10:
c = 'A';
break;
case 11:
c = 'B';
break;
case 12:
c = 'C';
break;
case 13:
c = 'D';
break;
case 14:
c = 'E';
break;
case 15:
c = 'F';
break;
default:
c = (char) (m+48);//加上48,因为ASCII编码 48 到 57 为字符 '0' ~ '9' 的编码
}
s.push(c);
i = i / 16;
}
while (!s.empty()) {
e = s.pop();
System.out.print(e);
}
}
/**
* 十进制转换为十六进制字符串
*
* @param algorism
* int 十进制的数字
* @return String 对应的十六进制字符串
*/
public static String to16(int algorism) {
String result = "";
result = Integer.toHexString(algorism);
if (result.length() % 2 == 1) {
result = "0" + result;
}
result = result.toUpperCase();
return result;
}
你好!采纳一下吧,我发你
package com;
/**
*
* @author Roc-xb
*
*/
public class Mian2 {
public static void main(String[] args) {
System.out.println("十进制\t二进制\t八进制\t十六进制");
for (int n = 25; n <= 31; n++) {
String bin = binaryToDecimal(n);
String oct = octal(n);
String hex = tohex(n);
System.out.println(String.format("%s\t%s\t%s\t%s\t", n, bin, oct, hex));
}
}
/**
* 十进制转二进制
*
* @param n
* @return
*/
public static String binaryToDecimal(int n) {
String bin = "";
while (n != 0) {
bin = n % 2 + bin;
n = n / 2;
}
return bin;
}
/**
* 十进制转八进制
*
* @param n
* @return
*/
public static String octal(int n) {
StringBuilder o = new StringBuilder();
while (n > 0) {
o.append(n % 8);
n = n / 8;
}
return o.reverse().toString();
}
/**
* 十进制转十六进制
*
* @param n
* @return
*/
public static String tohex(int n) {
String hexStr = "";
long decAbs = Math.abs(n);
while (decAbs > 0) {
long lastFour = decAbs & 15;
if (lastFour > 9) {
hexStr = (char) ('A' + lastFour - 10) + hexStr;
} else {
hexStr = lastFour + hexStr;
}
decAbs >>= 4;
}
hexStr = n < 0 ? "-" + hexStr : n == 0 ? "0" : hexStr;
return hexStr;
}
}