问题描述
我正在练习Try-Catch,异常处理以及堆栈和队列,因此尽管代码可能不切实际,但对我的练习很有用.
I am practicing Try-Catch, Exception Handling, and Stacks and Queues, so although the code may be impractical, it is useful for my practice.
public class Practice {
public static void main(String args[]){
Stack<String> sStack = new Stack<String>();
Queue<String> sQueue = new LinkedList<String>();
Scanner input = new Scanner(System.in);
String inString = " ";
boolean done = false;
do{
try{
while(!inString.equals("")){
System.out.println("Enter a string: ");
inString = input.nextLine().toString();
addS(sStack,inString);
}
done = true;
}catch(Exception e){}
sStack.pop();
for(int i = 0; i<sStack.size()+1;i++){
remS(sStack);
}
}while(done == false);
}
该代码旨在根据用户输入遍历并填充一个空堆栈,然后遍历该堆栈并删除每个元素,同时给出提示.我输入的第一组是:
The code is intended to loop through and populate an empty stack based on user input, and then loop through the stack and remove each element while giving a prompt. The first set I input is:
[艺术,蝙蝠,猫,船坞,眼睛,父亲]
[art, bat, cat, dock, eye, father]
第二组是:
[艺术,蝙蝠,猫,码头,眼睛,父亲,孩子]
[art, bat, cat, dock, eye, father, kid]
我注意到,无论输入多少元素,它最多只能删除4个元素.我希望您能帮助您找出原因.如您所见,我尝试过循环的边界,但无济于事.无论我在哪里开始或绑定,它最终都只会弹出最多4个元素.
I noticed that no matter how many elements I input it will only remove up to 4 elements. I would like your help in figuring out why. I tried playing around with the loop's bounds as you can see, but to no avail. No matter where I start or bound, it will always end up popping only 4 elements max.
我在下面提供的addS和remS函数
The addS and remS functions I provide below
public static void addS(Stack t, String s){
t.push(s);
System.out.printf("%s was added to the stack %s", s, t);
System.out.println();
}
public static void remS(Stack t){
System.out.printf("\n%s has been popped.",t);
t.pop();
System.out.printf("The current elements are now: ");
System.out.print(t);
System.out.printf("The next element to be popped is %s", t.peek());
}
推荐答案
这是一个常见问题.这是一个很好的机会,您首先可以学习如何使用调试器,其次可以学习如何从逻辑上考虑编程问题.
This is a frequently asked question. This is a good opportunity for you to first, learn how to use your debugger, and second, learn how to think about programming problems logically.
for(int i = 0; i<sStack.size()+1;i++){
remS(sStack);
}
这总是删除堆栈中一半的项目.通过在纸上模拟循环的操作来找出原因,您将很快了解原因.
This always removes half the items in the stack. Work out why by emulating the operation of the loop on paper and you will quickly see why.
我的意思的例子.写下:
Example of what I mean. Write down:
stack contains A, B, C, D
i is 0
test loop condition: 0 is smaller than 4 + 1, so we enter the loop.
remove item from stack.
stack contains B, C, D
size is 3
execute loop increment: i is now 1
test loop condition: 1 is smaller than 3 + 1, so we enter the loop...
完成它.
好的,现在您了解了问题,该如何解决?执行此操作的两种标准方法是:(1)首先计数,将结果保存在本地,然后对本地进行测试;(2)完全删除计数器,因为这是不必要的;而是在大小大于零时循环.我更喜欢后一种解决方案.突变的变量越少,对它们进行突变的错误就越少.
OK, now that you understand the problem, how are you going to fix it? The two standard ways to do this are: (1) count first, save the result in a local, test against the local, and (2) remove the counter altogether, since it is unnecessary; instead, loop while the size is greater than zero. I prefer the latter solution; the fewer variables you mutate, the fewer mistakes you'll make in mutating them.
这篇关于堆栈弹出循环不会弹出所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!