我已经调试了代码并发现它添加了第一个元素(堆栈大小= 1) 但是当尝试添加下一个元素时,堆栈大小跳回到 0
我认为问题出在公共(public)无效转换中。
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);
}
}
´´´
关于java - 为什么我的堆栈只添加一个元素然后又删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61733126/