Java Can';“推”方法无法工作
我需要一些快速完成作业的帮助,简而言之,我完成了一项作业,要求我制作一个程序,允许用户pop()、push()和top()数组 我这样做了,但是我的导师说代码布局错误,因为我在stack类中有System.out.println语句,这些语句应该在主菜单应用程序中,stack类应该只调用它从array类继承的方法 我认为这很公平,并修改了代码,但现在无法使push()方法正常工作:/ 我知道我需要在菜单app push()方法中使用Genio.getInteger方法 有人能帮忙吗 堆栈类:Java Can';“推”方法无法工作,java,arrays,Java,Arrays,我需要一些快速完成作业的帮助,简而言之,我完成了一项作业,要求我制作一个程序,允许用户pop()、push()和top()数组 我这样做了,但是我的导师说代码布局错误,因为我在stack类中有System.out.println语句,这些语句应该在主菜单应用程序中,stack类应该只调用它从array类继承的方法 我认为这很公平,并修改了代码,但现在无法使push()方法正常工作:/ 我知道我需要在菜单app push()方法中使用Genio.getInteger方法 有人能帮忙吗 堆栈类:
public class Stack extends Array
{
private int x;
public Stack()
{
super();// initialise instance variables
x = 0;
}
public Stack(int newsize)
{
super(newsize);
System.out.println("Stack Created!");
}
/**
* @push user is asked to enter value at keyboard which is then added to the top of the stack
*
*/
public boolean push(int item)
{
return add(item);
}
/**
* @pop removes the current value staored at the top of the stack
*
*/
public int pop()
{
deleteLast();
return getItemback();
}
/**
* @top displays the current value stored at the top of the stack
*
*/
public int top()
{
displayLast();
return getItemback();
}
}
菜单应用程序:
public static void main()
{
int option;
int item;
Stack s = new Stack();
String []menuitems = {"1 - Display Stack","2 - Pop Stack", "3 - Push Onto Stack","4 - Top Of Stack","5 - Quit Program"};
Menu m = new Menu(menuitems,5);
s.add(12);s.add(2);s.add(1);s.add(13);s.add(24);
do
{
clrscr();
option = m.showMenu();
if ( option == 1 )
{
s.display();
pressKey();
}
if ( option == 2 )
{
if (!s.isEmpty())
System.out.println ("Number Popped: ");
else
System.out.println ("The Stack Is Empty! ");
pressKey();
}
// THIS IS THE PART I CANNOT GET TO WORK!! NOT SURE WHERE/HOW TO CALL PUSH
// METHOD?
if ( option == 3 )
{
item = Genio.getInteger();
if (!s.isFull())
System.out.println("Please Enter Number To be Pushed(" + item + ")");
else
System.out.println("Stack Overflow! ");
pressKey();
}
if ( option == 4 )
{
if (!s.isEmpty())
s.top();
else
System.out.println ("The Stack Is Empty! ");
pressKey();
}
}
while ( option != 5 );
System.out.println("\nDone! \n(You Can Now Exit The Program)\n");
}
/**
* @clrscr removes all text from the screen
*
*/
public static void clrscr()
{
for ( int i=1;i<=50;i++)
System.out.println();
}
/**
* @pressKey requires the user to press return to continue
*
*/
public static void pressKey()
{
String s;
System.out.print("\nPress return to continue : ");
s = Genio.getString();
}
}
{
}不确定我是否正确理解了你的问题,但没有呼叫s.push?(s.pop也不是吗?) 如果你愿意替换
if (!s.isFull())
与
一些工作会完成吗
如果您只想让它成为一个提示,请使用花括号(无论如何都要使用它们,总有一天它会让您免于可怕的bug)。像这样的
if (!s.isFull()) {
System.out.println("enter a number to add");
Scanner sc = new Scanner(System.in);
s.push(sc.nextInt());
}
我添加了Genio类代码。我不完全确定,除了它处理用户输入。我的导师只告诉我“你需要在菜单应用程序中使用Genio进行推送”。推送方法叫什么?是的,我在“选项3”中缺少推送方法,这是因为我一直在尝试不同的方法来让它工作,但在发布之前忘了添加它。。。。我现在可能真的很愚蠢,但是这个数组类在标准库中吗?因为我在搜索时找不到它…我说不出来。对不起,我才刚开始。
if (!s.isFull())
if (!s.isFull() && s.push(item))
if (!s.isFull()) {
System.out.println("enter a number to add");
Scanner sc = new Scanner(System.in);
s.push(sc.nextInt());
}