簡體   English   中英

通過用戶輸入在數組中添加和排序元素

[英]Adding and sorting elements in array through user input

我的程序在第一個元素進入數組后停止。 你可以幫幫我嗎? 這是代碼:對不起,我已經做了一些更改現在ArrayQueue是我的類,其中我的所有方法都被實現QueueElement是另一個類,它包含元素的類型..我認為問題現在是在ArrayQueue類

              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;
                      }

不知道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聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM