当我用来运行这个程序时我遇到问题。这是不断重复案例1 .`
import java.util.Scanner; //importing
public class DynamicStackByArray { // name of class
private static String[] a;
private static int size;
public DynamicStackByArray(){} // end of the null constructor
public DynamicStackByArray(int capacity){
a=new String[capacity];
}// end of the parameterized constructor
private static String peek(){
if(size==0)
throw new IllegalStateException("Stack is Empty");
else
return a[size-1];
}// end of the displaying method
private static String pop(){
if(size==0)
throw new IllegalStateException("Stack is Empty...");
else{
String o=a[--size];
a[size]=null;
return o;
}
}// end of the pop method
private static void push(String o){
if(size==a.length)resize();
else
a[size++]=o;
}// end of the push method
private static void resize(){
String aa[]=a;
a=new String[1+aa.length];
System.arraycopy(aa, 0, a, 0, size);
aa=null;
}// end of the resize method
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of dynamic stack :");
int cap=in.nextInt();
DynamicStackByArray d=new DynamicStackByArray(cap);
System.out.println("Enter your choice \n1) for push\n2) for pop\n3) for peek");
System.out.println("Enter -999 to exit");
int choice=in.nextInt();
while(choice!=-999){
switch(choice){
case 1:{
System.out.println("Enter your desire data");
System.out.println("Note that -999 will let u escape out of it... :)");
String input;
input=in.nextLine();
String as="-999";
if(!input.equalsIgnoreCase(as)){
d.push(input);
}// end of checker
System.out.println("Enter your choice \n1) for push\n2) for pop\n3) for peek");
System.out.println("Enter -999 to exit");
choice=in.nextInt();
break;
}// end of case 1
case 2:{
System.out.println("Value "+d.peek()+" has been poped ");
d.pop();
System.out.println("Enter your choice \n1) for push\n2) for pop\n3) for peek");
System.out.println("Enter -999 to exit");
choice=in.nextInt();
break;
}// end of the case 2
case 3:{
System.out.println("Value "+d.peek()+" has been peeked");
System.out.println("Enter your choice \n1) for push\n2) for pop\n3) for peek");
System.out.println("Enter -999 to exit");
choice=in.nextInt();
break;
}// end of the case 3
default:{
System.out.println("Sorry this is a wrong choice...");
System.out.println("Enter your choice \n1) for push\n2) for pop\n3) for peek");
System.out.println("Enter -999 to exit");
choice=in.nextInt();
break;
}// end of default
}// end of the switch statement
}// end of the repetation of the program
System.out.println("GOOD BYE... !!!");
}// end of the main method
}// end of the class
`
答案 0 :(得分:1)
你忘了询问扫描仪在你的循环内输入。你可以解决它:
int choice = 0;
while ((choice = in.nextInt()) != -999) {
//... your loop
}
更重要的是:
在case 1:
中,当您阅读input=in.nextLine();
时,它将消耗用于终止" 1"的\n
符号输入,您的String input
将始终为空字符串。 (只需输入断点并自行验证)
因此,您必须在案例1中添加额外的nextLine()
语句:
in.nextLine(); // read `\n` symbol;
String input = in.nextLine();
答案 1 :(得分:0)
它不是一直只执行case1。该程序运行正常。你在两种情况下都采用peek方法犯了错误,你应该在case2中使用pop方法作为你想要的。