如何保存/显示队列值?

时间:2016-06-15 08:40:30

标签: java eclipse windows-7

我还是Java的新手,所以我不知道如何创建一个好的主类。在下面的脚本中,我为队列程序制作了一个示例主类。如果我从语法中手动输入它(Queue的值),它会保存在程序中并显示在控制台上。但是如果我从控制台输入,它就不会被保存在程序中。

package kelompok;

import java.util.Scanner;
import kelompok.Queue;

class QGUI{

    private Scanner inp;

    public void qgui(){
    {
        int pilih, enq, yes=0;      

        inp = new Scanner(System.in);
        Queue theQueue = new Queue(100);

        try{while (yes == 0){
        System.out.println("1. Enqueue");
        System.out.println("2. Denqueue");
        System.out.print("Masukkan perintah (1/2): ");
        pilih = inp.nextInt();


        if (pilih == 1){
            System.out.print("Masukkan queue: ");
            enq = inp.nextInt();
            theQueue.enqueue(enq);
        }
        else if (pilih == 2){

            theQueue.dequeue();         
        }
        while( !theQueue.isEmpty() )    // remove and display
        {                            //    all items
        long n = theQueue.dequeue();  // (40, 50, 60, 70, 80)
        System.out.print(n);
        System.out.print(" ");
        }
        System.out.println("");

       // end main()
}
}catch(Exception e){
    System.out.println("Data yang dimasukan bukan angka(Integer)!");
    System.out.println("Program harus di-restart!");
    System.exit(100);
    }
}
}
}

队列类

package kelompok;

public class Queue
{
private int maxSize = 100;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//————————————————————–
public Queue(int s)          // konstruktor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//————————————————————–
public void enqueue(long r)   // letakkan item (data) di posisi belakang dari queue
{
if(rear == maxSize-1)         //
rear = -1;
queArray[++rear] = r;         //naikkan rear dan masukkan item (data) pada posisi rear yang baru
nItems++;                     //tambah satu item lagi
}
//————————————————————–
public long dequeue()         // hapus item (data) yang berada pada posisi front
{
long temp = queArray[front++]; //dapatkan nilainya dan naikkan front
if(front == maxSize)           //
front = 0;
nItems-- ;                      // item (data) berkurang satu
return temp;
}
//————————————————————–
public long peekFront()      //
{
return queArray[front];
}
//————————————————————–
public boolean isEmpty()    //benar jika queue-nya kosong
{
return (nItems==0);
}
//————————————————————–
public boolean isFull()     // benar jika queue-nya penuh
{
return (nItems==maxSize);
}
//————————————————————–
public int size()           // jumlah ietm (data) dalam queue
{
return nItems;
}
//————————————————————–
public int clear()           // jumlah ietm (data) dalam queue
{
return (nItems = -1);
}

public void Display(){
System.out.println("Isi Queue Adalah :  ");
//printing list item
for(int i=0; i<nItems; i++){
System.out.print(queArray[(front + i) % maxSize]+" ");
}
System.out.println("\n");
}//end of Display

}  // end class Queue

1 个答案:

答案 0 :(得分:0)

如果要在输入后显示队列元素,则必须在条件内添加Display方法。请查看以下内容:

public void qgui() {
    {
        int pilih, enq = 0;
        inp = new Scanner(System.in);
        Queue theQueue = new Queue(5);
        try {
            while (theQueue.size() < 5) {
                System.out.println("1. Enqueue");
                System.out.println("2. Denqueue");
                System.out.print("Masukkan perintah (1/2): ");
                pilih = inp.nextInt();

                if (pilih == 1) {
                    System.out.print("Masukkan queue: ");
                    enq = inp.nextInt();
                    theQueue.enqueue(enq);
                    theQueue.Display(); // Once you add if you want to display you have to add this
                } else if (pilih == 2) {
                    while (!theQueue.isEmpty()) // In dequeue remove and display
                    { // all items
                        long n = theQueue.dequeue(); // (40, 50, 60, 70, 80)
                        System.out.print(n);
                        System.out.print(" ");
                    }
                }   
            }
        } catch (Exception e) {
            System.out.println("Data yang dimasukan bukan angka(Integer)!");
            System.out.println("Program harus di-restart!");
            System.exit(100);
        }
        while (!theQueue.isEmpty()) // NOTE - remove and display
        { // all items
            long n = theQueue.dequeue(); // (40, 50, 60, 70, 80)
            System.out.print(n);
            System.out.print(" ");
        }
        System.out.println("");
    }
}