作者chiunyao (..)
看板java
标题[问题] 谁可以帮我看一下传回值哪出问题了 -STACK
时间Tue Apr 11 14:40:36 2006
这事实做一个STACK 新手多多见谅~~
我刚开始设定 TOP=-1
可是只要呼叫到 push(s,num2,top);
PUSH函数里面有top++
可是婐是过了~~怎麽样丢回来 top的值都还是在-1 根本没有++
请问是哪出错了呢~~
谢谢罗~
import java.io.*;
public class STACK
{
public static void main(String args[]) throws IOException
{
//宣告阵列
int s[];
s=new int[10];
//设定TOP指标
int top=-1;
int num1,num2;
int i,j,k;
BufferedReader buf;
String str1,str2;
buf=new BufferedReader(new InputStreamReader(System.in));
for(k=0;k<10;k++)
{
System.out.print("请选择1.PUSH 2.POP: ");
str1=buf.readLine();
num1=Integer.parseInt(str1);
if(num1==1)
{
System.out.print("请输入一个数字:");
str2=buf.readLine();
num2=Integer.parseInt(str2);
push(s,num2,top);
}
else if(num1==2)
{
pop(s,top);
}
else
System.out.printf("输入错误,请输入1.2!!");
for(i=0;i<s.length;i++)
System.out.print("s["+i+"]="+s[i]+",\t");
}
}
public static int push(int arr[],int val,int top)
{
if(top>=9)
{
System.out.println("Stack is Full.");
}
else
{
top++;
arr[top]=val;
}
return top;
}
public static int pop(int arr[],int top)
{
if(top<0)
{
System.out.println("Stack is empty");
}
else
{
top--;
}
return arr[top];
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.134.89.94