1

キューを作成し、オブジェクトをキューにエンキューし、キューが空でない場合はそれらを1つずつデキューするプログラムがあります。私が抱えている問題は、チェックされるたびにキューが空になることです。Print は、各オブジェクトをキューに入れた後にキューで呼び出され、キューの内容を正常に出力します。

import java.util.*;
import java.io.*;
public class Processor2
{
private LinkedQueue queue = new LinkedQueue();
private int time = 0;
private int count = 100;
private int amount = 0;
private PrintWriter out;
private Person temp;
private boolean var;
private Random randomNum = new Random();;
private String turn;
private int popCount=0;
private int loopCount =0;

public void start()
{
    amount = randomNum.nextInt(5);
    amount += 5;
    pop(amount, time);
    sim();
}

public void pop(int num, int time)
{

        for(int i=1; i<=num; i++)
        {
            Person pe = new Person(i, time, 0);
            queue.enqueue(pe);
            System.out.println(queue);
        }   
        popCount += num;
}

public void sim()
{
    try
    {
        out = new PrintWriter(new FileWriter("output.txt")); 

        while(loopCount<=100)
        {
            var = queue.isEmpty();
            if(var=true)
            {
                System.out.println("queue is empty");
            }

            if(var=false)
            {
                Object temp = queue.dequeue();
                double rand = Math.random();

                if(rand < 0.5)
                {
                    System.out.println("inside if else statement");
                    // does stuff with object //
                    loopCount++;
                }
                else
                {
                    System.out.println("inside if else statement");
                    // does stuff with object //
                    loopCount++;
                }
            }
        }
        out.close();
    }
    catch (IOException ioe)
    {
        System.out.println("Error Writing to File: " + ioe);
    }
}
}

キューの isEmpty() メソッドに問題はないようですが、次のとおりです。

public boolean isEmpty() 
{
    if(count == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
4

1 に答える 1

10
var = queue.isEmpty();
if(var=true) // this line *sets* var to true
{
    System.out.println("queue is empty");
}

またはに変更if (var=true)すると、別の結果が表示されるはずですif(var==true)if(var)

于 2012-04-13T19:00:57.013 に答える