我已经调试了代码并发现它添加了 fist 元素(堆栈大小 = 1( 但是当尝试添加下一个元素时,堆栈大小跳回到 0
我认为问题出在公共无效转换上。
public void convert(int N) {
this.N = N;
int binary[] = new int[10];
int index = 0;
while (N > 0) {
binary[index++] = N % 2;
N = N / 2;
}
for (int x = index - 1; x >= 0; x--) {
this.binStack.push(binary[x]); //Here is the Problem
}
}
这是整个代码:
import java.util.Stack;
public class Dec2Bin {
public Stack<Integer> binStack; // We make it public to modify it in our tests.
private int N;
/**
* Constructor of an empty object. Use method {@code convert()} to convert a number.
*/
public Dec2Bin() {
binStack = new Stack<>();
}
/**
* Returns the number that is converted as {@code int}.
*
* @return the converted number
*/
public int getN() {
return N;
}
/**
* Converts the given number into binary format, with each digit being represented in a
* stack of {@code int}.
*
* @param N the number that is to be converted.
*/
public void convert(int N) {
// TODO implement this method
this.N = N;
int binary[] = new int[10];
int index = 0;
while (N > 0) {
binary[index++] = N % 2;
N = N / 2;
}
for (int x = index - 1; x >= 0; x--) {
this.binStack.push(binary[x]);
}
}
/**
* Returns the digits that are stored in {@code binStack} as a string. To is the binary format of the
* converted number.
* For testing purpose, we require that the function works also, if the variable {@code binStack} is
* modified externally.
*
* @return a string representation of the number in binary format.
*/
@Override
public String toString() {
// Caution: Stack.toString() does NOT respect stack order. Do not use it.
// TODO implement this method
String finalBinär = new String();
for (int y = 0; y < binStack.size(); y++){
String binär = Integer.toString(binStack.pop());
String prevBinär = new String();
finalBinär = prevBinär.concat(binär);
}
return finalBinär;
}
public static void main(String[] args) {
Dec2Bin dec2bin = new Dec2Bin();
dec2bin.convert(50);
System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
// Do it another time to demonstrate that toString does not erase the binStack.
System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
}
}
´´´
import java.util.Stack;
public class Dec2Bin {
public Stack<Integer> binStack; // We make it public to modify it in our tests.
private int N;
/**
* Constructor of an empty object. Use method {@code convert()} to convert a number.
*/
public Dec2Bin() {
binStack = new Stack<>();
}
/**
* Returns the number that is converted as {@code int}.
*
* @return the converted number
*/
public int getN() {
return N;
}
/**
* Converts the given number into binary format, with each digit being represented in a
* stack of {@code int}.
*
* @param N the number that is to be converted.
*/
public void convert(int N) {
// TODO implement this method
this.N = N;
int binary[] = new int[10];
int index = 0;
while (N > 0) {
binary[index++] = N % 2;
N = N / 2;
}
for (int x = index - 1; x >= 0; x--) {
this.binStack.push(binary[x]);
}
}
/**
* Returns the digits that are stored in {@code binStack} as a string. To is the binary format of the
* converted number.
* For testing purpose, we require that the function works also, if the variable {@code binStack} is
* modified externally.
*
* @return a string representation of the number in binary format.
*/
@Override
public String toString() {
// Caution: Stack.toString() does NOT respect stack order. Do not use it.
// TODO implement this method
String finalBinär = new String();
for (int y = 0; y < binStack.size(); y++){
String binär = Integer.toString(binStack.pop());
String prevBinär = new String();
finalBinär = prevBinär.concat(binär);
}
return finalBinär;
}
public static void main(String[] args) {
Dec2Bin dec2bin = new Dec2Bin();
dec2bin.convert(50);
System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
// Do it another time to demonstrate that toString does not erase the binStack.
System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
}
}
´´´
您的toString()
方法至少有两个问题:
@Override
public String toString() {
// Caution: Stack.toString() does NOT respect stack order. Do not use it.
// TODO implement this method
String finalBinär = new String();
for (int y = 0; y < binStack.size(); y++){ // <1>
String binär = Integer.toString(binStack.pop()); // <2>
String prevBinär = new String();
finalBinär = prevBinär.concat(binär); // <3>
}
return finalBinär;
}
第 <1>和 <2> 行:在 for 循环期间,y
在第 <1> 行上递增。同时堆栈大小减小,因为在第 <2> 行上,您使用binStack.pop()
从堆栈中删除元素。
这意味着您只会得到大约一半的数字。(例如,如果binStack.size()
开始时是 6:在 3 次运行后通过循环y == 3
和binStack.size() == 3
,那么您的循环将终止。
第 <3> 行意味着无论binStack
中的内容是什么,您的结果都将只包含一个二进制数字:该行(与前一行一起(与finalBinär = ""+binär;
具有相同的效果,这实际上意味着:finalBinär = binär;