简体   繁体   English

在 do-while 循环中输入不适用于扫描仪 class

[英]Input not working with Scanner class in the do-while loop

In the do-while loop,the code is not taking input after first case.在 do-while 循环中,代码在第一种情况后不接受输入。 it gets terminated after taking the input and does not performs do-while loop properly.what is wrong with z=sc.nextLine(){in the while loop} or Scanner class?它在接受输入后被终止并且没有正确执行 do-while 循环。z=sc.nextLine(){in the while loop} 或扫描仪 class 有什么问题? PS: I also tried with Scanner sc=new Scanner("System.in"); PS:我也试过 Scanner sc=new Scanner("System.in"); but the code still didn't work.但代码仍然不起作用。 Any other alternative than parseInt()?除了 parseInt() 还有其他选择吗?

OUTPUT COMING: size of stack? OUTPUT 即将到来:堆栈大小? 5 1.PUSH 2.POP 3.Display 1 number for push? 5 1.PUSH 2.POP 3.显示1个数字推送? 19 continue? 19继续? after continue,code gets terminated继续后,代码被终止

Please Help.请帮忙。

class stak{
   int tos=-1;
   int size;
   int a[];
   
   stak(int l){
       size=l;
       tos=-1;
       a=new int[l];
   }
   void push(int x){
       if(tos==size-1){
           System.out.println("Stack Overflow");
       }
       else{
           a[++tos]=x;
       }
   }
   void pop(){
       if(tos<0){
           System.out.println("Stack Underflow");
       }
       else{
           System.out.println(a[tos--]);
       }
   }

   void display(){
       if(tos>=0){
           for(int i=tos;i>=0;--i){
               System.out.println(a[i]);
           }
       }
       else
       System.out.println("Stack is Empty");
   }

}

public class stack{
   public static void main(String args[]){
      Scanner sc=new Scanner(System.in);
      System.out.println("size of stack?");
      int n=sc.nextInt();
      stak s1=new stak(n);
      String z;
      do{
      System.out.println("1.PUSH");
      System.out.println("2.POP");
      System.out.println("3.Display");
      int ch=sc.nextInt();
      
      switch(ch){
           case 1 : System.out.println("number for push?");
                   int p=sc.nextInt();
                   s1.push(p);
                   break;
                   case 2 : System.out.println("POP!");
                   s1.pop();
                   break;
                   case 3 : System.out.println("Display!");
                   s1.display();
                   break;
                   default: System.out.println(" OOPS! owner says only take input as: 1,2,3");
                   break;
      }
     System.out.println("continue?");
    z=sc.nextLine(); 
   }while(z.equals("yes")|| z.equals("y"));
   sc.close();
}
}


 [1]: https://i.stack.imgur.com/BHZQO.png

The problem is with the scanner class.问题出在扫描仪 class 上。 Because when you use call z=sc.nextLine();因为当你使用 call z=sc.nextLine(); this call first take the input from the line where you enter the number to push into the stack because int p=sc.nextInt();此调用首先从您输入要压入堆栈的数字的行中获取输入,因为int p=sc.nextInt(); this line only take one integer but does not finish the whole line.这条线只取一根 integer 但没有走完整条线。

So you can manually finish the line by adding a sc.nextLine() before taking the input for yes or no.因此,您可以在输入是或否之前通过添加sc.nextLine()手动完成该行。 By adding the extra nextLine() will finish the current line when you inter the integer.通过添加额外的 nextLine() 将在您插入 integer 时完成当前行。

import java.util.Scanner;

class stak{
    int tos=-1;
    int size;
    int a[];
    
    stak(int l){
        size=l;
        tos=-1;
        a=new int[l];
    }
    void push(int x){
        if(tos==size-1){
            System.out.println("Stack Overflow");
        }
        else{
            a[++tos]=x;
        }
    }
    void pop(){
        if(tos<0){
            System.out.println("Stack Underflow");
        }
        else{
            System.out.println(a[tos--]);
        }
    }
 
    void display(){
        if(tos>=0){
            for(int i=tos;i>=0;--i){
                System.out.println(a[i]);
            }
        }
        else
        System.out.println("Stack is Empty");
    }
 
 }
 
 public class stack{
    public static void main(String args[]){
       Scanner sc=new Scanner(System.in);
       System.out.println("size of stack?");
       int n=sc.nextInt();
       stak s1=new stak(n);
       String z;
       do{
       System.out.println("1.PUSH");
       System.out.println("2.POP");
       System.out.println("3.Display");
       int ch=sc.nextInt();
       switch(ch){
            case 1 : System.out.println("number for push?");
                    int p=sc.nextInt();
                    s1.push(p);
                    break;
                    case 2 : System.out.println("POP!");
                    s1.pop();
                    break;
                    case 3 : System.out.println("Display!");
                    s1.display();
                    break;
                    default: System.out.println(" OOPS! owner says only take input as: 1,2,3");
                    break;
       }
      System.out.println("continue?");
      sc.nextLine();
      z=sc.nextLine(); 
    }while(z.equals("yes")|| z.equals("y"));
    sc.close();
 }
 }

Hope this will help you.希望这会帮助你。 Other wise you can counter this issue by using BufferReader否则,您可以使用 BufferReader 解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM