[問題] 誰可以幫我看一下傳回值哪出問題了 -STACK
這事實做一個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
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章