gpt4 book ai didi

java - 如何从堆栈中删除某些项目

转载 作者:行者123 更新时间:2023-11-29 07:11:41 25 4
gpt4 key购买 nike

<分区>

我有一个家庭作业问题,我需要从堆栈中移除所有绿色项目。

这是我的代码:

import java.util.*;
import java.io.*;

public class Pex
{
public static void main(String[] args)
{
Stack stack1 = new Stack();

addPez(stack1);
removeGreen(stack1);
System.out.println(printStack(stack2));
}

public void addPez(Stack stack1)
{
stack1.push("yellow");
stack1.push("red");
stack1.push("green");
stack1.push("green");
stack1.push("yellow");
stack1.push("yellow");
stack1.push("red");
stack1.push("green");
}

public static void removeGreen(Stack stack1)
{
Stack temp = new Stack();
while (!stack1.isEmpty()){
String check = (String)(stack1.pop());
if(check.equals("green")){
stack1.pop();}
else{
temp.push(check);}
}
Stack stack2 = new Stack();
while (!temp.isEmpty()){
String tmp = stack1.pop();
stack2.push(tmp);}
}

public static void printStack(Stack stack2)
{
Stack xerox = stack2.clone();
while (!xerox.isEmpty()){
System.out.println(xerox.pop());}
}
}

有人能指出我正确的方向吗?我想我快到了。

我还需要弄清楚为什么会出现一些错误。

错误如下:

Pex.java:10: non-static method addPez(Stack) cannot be referenced from a static context
addPez(stack1);
^
Pex.java:12: cannot find symbol
symbol : variable stack2
location: class Pex
System.out.println(printStack(stack2));
^

Pex.java:39: incompatible types
found : java.lang.Object
required: java.lang.String
String tmp = stack1.pop();
^

Pex.java:45: incompatible types
found : java.lang.Object
required: Stack
Stack xerox = stack2.clone();
^

./Stack.java:69: cannot find symbol
symbol : variable stack1
location: class Stack
stack2 = (Stack)(stack1.clone());
^

5 errors

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com