0

我的程序在第一个元素进入数组后停止。你可以帮帮我吗?这是代码:抱歉,我现在已经做了一些更改

              import java.io.*;
             import java.util.Scanner;
           public class QueueTest
          { private static  Scanner scan =new Scanner(System.in);
               public static void main(String[] args)
                   {
              ArrayQueue queue = new ArrayQueue();
              System.out.println("insert :" +
            " P: to print the array "+
            "S: to sort "+
            "E: to empty"); 
            do
             {
        if (userInput.startsWith("Y")) {
            System.out.println("insert the value of element");
            int vl = scan.nextInt();
            System.out.println("insert the description");
            String p = scan.next();
            System.out.println("insert true or false");
            boolean vlf = scan.nextBoolean();
            queue.shtoElement(new QueueElement(vl, p, vlf));
        } else if (userInput.startsWith("P")) {
            queue.shfaqArray();//display the array
        } else if (userInput.startsWith("S")) {
            queue.rendit();//sort the array
        } else if (userInput.startsWith("E")) {
            queue.Empty();//empty array
        } 
        System.out.println("Insert:" + " P: to display the array "
                + " S: to sort array "
                + " E: to empty");
        userInput = scan.next();
        } while (!userInput.equalsIgnoreCase("exit"));
              }
                 }

这是类 ArrayQueue

                 public class ArrayQueue implements Queue

                             {
                //build the queue
          QueueElement[] tabela= new QueueElement[MADHESIA];
            QueueElement element= new QueueElement();
          //test if queue is empty
             public boolean isEmpty()
                {

               boolean empty = true;
                 for(int k =0;k<tabela.length;k++)
                      {
                            if(tabela[k] !=null)
                        empty =false;
                   break;
                                }
                            return empty;
                                          }

            public boolean isFull()
                {
                    boolean full=false;
                    for(int k=0;k<tabela.length;k++)
            {
        if(tabela[k]!=null)
            full=true;
        break;
            }
    return full;
        }

    //sort the array
        public void rendit()
           { 
    QueueElement temp=tabela[0];
    for(int i=0;i<tabela.length;i++)
    {
        for(int j=i+1;j<tabela.length;j++)
            if(tabela[j]!=null)
            {
            if(tabela[j].compareTo(tabela[i])==1)
            {
                tabela[j]=temp;
                tabela[i]=tabela[j];
                temp=tabela[i];
            }
            else 
                {
                nrElementeve++;
                }
            }
        if(tabela[i].getVlefshmeria()==false)
        {
            hiqElement(i,temp);
             }
            }
               }
    // add element into the array

           public void shtoElement(QueueElement el)
                  {     
                if(isEmpty())
        {
        tabela[0]=el;
        }
    else
         {
            for(int i=0;i<tabela.length;i++) 
               {    
                   if(tabela[i]!= null && (el.compareTo(tabela[i])==0))
                       {
    System.out.println("element can't be added into array cause it exists !");
                        }              
                   {
                       if(isFull())
                       {                        
                            int index=tabela.length;
                            tabela=rritMadhesine(tabela);
                            tabela[index]=el;
                     }  
                 else
                 {
            for(int j=0;j<tabela.length;j++)
        {
        if(tabela[j]==null)
                  tabela[j]=el;
        break;
        }
                        }
               }
         }
               }

             } 
       //find max of array

         public QueueElement gjejMax()
            {
          QueueElement max = tabela[0];
           for (QueueElement element :tabela)
              {
    if(element.getValue() > max.getValue())
        {
        max =element;
        }
    return max;
           }
          return max;
                } 
            //find min of array
            public QueueElement gjejMin()
                  {
                    QueueElement min= tabela[0];
                     for(QueueElement element : tabela)
                  {
                  if(element.getValue()< min.getValue())
               {
                    min=element;
                           }
                        return min;
                              }
                              return min;
                                }

             //remove element from array

             public void hiqElement(int indeksi, QueueElement temp) 
                   {
                      if(tabela.length > 0)
                      {
                  if(gjejMax().compareTo(temp)==0 || gjejMin().compareTo(temp)==0)
               {
                   System.out.println("element can't be removed!");
                   }
                     else
            {
                  for(int i=indeksi;i<tabela.length;i++)
                        {
                      tabela[i]=tabela[i+1];
                               }
                    }
                          nrElementeve--;
                                 }
                          }

        //empty array

           public void Empty()
                {
            tabela= new QueueElement[MADHESIA];
                           }

                       //display the array

                  public  void shfaqArray()
                     {
              for(int i=0;i< tabela.length;i++)
               {
              if(tabela[i]!=null)
             {
                System.out.println(tabela[i]);
            }
                else
      break;
                  }
                }

         //increase the length of array

         public static QueueElement [] rritMadhesine(QueueElement[] array){  
            QueueElement[] tab=new QueueElement[array.length+2];  
              return tab; 
                    }
                     private int nrElementeve;
                 private static final int MADHESIA = 10;
                      }
4

1 回答 1

2

不知道是什么ArrayQueueQueueElement但是...

似乎您正在阅读用户输入,然后通过决策树来根据该输入决定要做什么。但在每一点,您都在重新阅读用户输入:

     if(scan.next().startsWith("Y"))
     {
        System.out.println("Jepni vleren e elem");
        vl=scan.nextInt();
        System.out.println("Jepni pershkrimin  e elem");
        p=scan.next();
        System.out.println("Jepni vlefshmerine e elem");
        vlf=scan.nextBoolean();
        queue.shtoElement(new QueueElement(vl,p,vlf));
     }
     else
     {
        if(scan.next().startsWith("P"))
           queue.shfaqArray();
     }
     if(scan.next().startsWith("E"))
        queue.Empty();
     if(scan.next().startsWith("S"))
        queue.rendit();
     else
     {
        break;
     }

我认为您想要更多类似的东西:

     String userInput = scan.next();
     if(userInput.startsWith("Y"))
     {
        System.out.println("Jepni vleren e elem");
        vl=scan.nextInt();
        System.out.println("Jepni pershkrimin  e elem");
        p=scan.next();
        System.out.println("Jepni vlefshmerine e elem");
        vlf=scan.nextBoolean();
        queue.shtoElement(new QueueElement(vl,p,vlf));
     }
     else if(userInput.startsWith("P"))
        queue.shfaqArray();
     else if(userInput.startsWith("E"))
        queue.Empty();
     else if(userInput.startsWith("S"))
        queue.rendit();
     else
     {
        break;
     }

您在哪里读取用户输入一次 ( String userInput = scan.next();),然后根据userInput变量决定要做什么,而不是每次都重新扫描。此外,看起来您可能else在中间的某个地方遗漏了几个语句。

于 2012-07-30T14:35:16.703 回答