2012-04-02 13 views
0

次のコードはJavaで使用できますか?この行をコメントアウトしない限り、私のプログラムは動作しません。ifの複数の& &文が許可されているかどうかはわかりません。複数の&&ステートメントをJavaで使用できますか?

if(number.isBusy() && number2.isBusy() && number3.isBusy() && number4.isBusy() && number5.isBusy()) {} 

編集を追加する: をあなたは上記の与えられた行をコメントアウトした場合(これに追加何かを得る唯一のことは、「number5」ですが)あれば、プログラムが実際に出力ウィンドウで何かを表示しますそれが残っていると、すべての出力がゼロになります。

これは今全体のプログラムである。

// FILE: Line.java 
// This program illustrates the use of the lineSimulate method which uses 
// a simple queue to simulate cars waiting at a car wash. 

import java.util.LinkedList; 
import java.util.Queue; 
import java.util.Scanner; 
// import edu.colorado.simulations.BooleanSource; 
// import edu.colorado.simulations.Line ; 
// import edu.colorado.simulations.Averager; 

/****************************************************************************** 
* The <CODE>CarWash</CODE> Java application illustrates the use of 
* the <CODE>lineSimulate</CODE> method. 
* The illustration uses the following values: 
* <CODE> 
* <br>lineTime = 240 
* <br>arrivalTime = 0.0025 
* <br>totalTime = 6000 
* </CODE> 
* 
* <p><dt><b>Java Source Code for this class:</b><dd> 
* <A HREF="../applications/CarWash.java"> 
* http://www.cs.colorado.edu/~main/applications/CarWash.java 
* </A> 
* 
* @author Michael Main 
* <A HREF="mailto:[email protected]"> ([email protected]) </A> 
* 
* @version 
* Jun 12, 1998 
******************************************************************************/ 
public class LineWait 
{ 
    /** 
    * The main method activates <CODE>lineSimulate</CODE> with the values: 
    * <CODE> 
    * <br>lineTime = 240 
    * <br>arrivalTime = 0.0025 
    * <br>totalTime = 6000 
    * </CODE> 
    * <BR>The <CODE>String</CODE> argument (<CODE>args</CODE>) is not used in 
    * this implementation. 
    **/ 
    public static void main(String[ ] args) 
    { 

     final int LINETIME = 100 + (int)(Math.random() * ((400) + 1)); 
     final double ARRIVALPROB = (Math.random() * ((.1) + 1)); 
     final int TOTALTIME = 6000; 

     lineSimulate(LINETIME, ARRIVALPROB, TOTALTIME); 
    } 

    /** 
    * Simulate the running of a car washer for a specified amount of time. 
    * @param <CODE>lineTime</CODE> 
    * the number of seconds required to wash one car 
    * @param <CODE>arrivalProb</CODE> 
    * the probability of a customer arriving in any second, for example 
    * 0.1 is 10% 
    * @param <CODE>totalTime</CODE> 
    * the total number of seconds for the simulation 
    * <dt><b>Precondition:</b><dd> 
    * <CODE>lineTime</CODE> and <CODE>totalTime</CODE> are positive; 
    * <CODE>arrivalProb</CODE> lies in the range 0 to 1. 
    * <dt><b>Postcondition:</b><dd> 
    * The method has simulated a car wash where <CODE>lineTime</CODE> is the 
    * number of seconds needed to wash one car, <CODE>arrivalProb</CODE> is 
    * the probability of a customer arriving in any second, and 
    * <CODE>totalTime</CODE> is the total number of seconds for the 
    * simulation. Before the simulation, the method has written its three 
    * parameters to <CODE>System.out</CODE>. After the simulation, the method 
    * has written two pieces of information to <CODE>System.out</CODE>: 
    * (1) The number of cars washed, and (2) The average waiting time for 
    * customers that had their cars washed. (Customers that are still in the 
    * queue are not included in this average). 
    * @exception java.lang.IllegalArgumentException 
    * Indicates that one of the arguments violates the precondition. 
    **/ 
    public static void lineSimulate 
    (int lineTime, double arrivalProb, int totalTime) 
    { 
     Queue<Integer> arrivalTimes = new LinkedList<Integer>(); 
     Queue<Integer> arrivalTimes2 = new LinkedList<Integer>(); 
     Queue<Integer> arrivalTimes3 = new LinkedList<Integer>(); 
     Queue<Integer> arrivalTimes4 = new LinkedList<Integer>(); 
     Queue<Integer> arrivalTimes5 = new LinkedList<Integer>(); 
     int next; 
     BooleanSource arrival = new BooleanSource(arrivalProb); 
     Line number = new Line(lineTime); 
     Line number2 = new Line(lineTime); 
     Line number3 = new Line(lineTime); 
     Line number4 = new Line(lineTime); 
     Line number5 = new Line(lineTime); 
     Averager waitTimes = new Averager(); 
     Averager waitTimes2 = new Averager(); 
     Averager waitTimes3 = new Averager(); 
     Averager waitTimes4 = new Averager(); 
     Averager waitTimes5 = new Averager(); 
     int currentSecond; 
     // Write the parameters to System.out. 
     System.out.println("Seconds to wait in line " + lineTime); 
     System.out.print("Probability of customer arrival during a second: "); 
     System.out.println(arrivalProb); 
     System.out.println("Total simulation seconds: " + totalTime); 

     // Check the precondition: 
     if (lineTime <= 0 || arrivalProb < 0 || arrivalProb > 1 || totalTime < 0) 
     throw new IllegalArgumentException("Values out of range");   




     for (currentSecond = 0; currentSecond < totalTime; currentSecond++) 
     { // Simulate the passage of one second of time. 

     // Check whether a new customer has arrived. 
     if (arrival.query()) 
      { 
       System.out.println("Test1"); 
       if(number.isBusy() && number2.isBusy() && number3.isBusy() && number4.isBusy() && number5.isBusy()) 
       { 
        System.out.println("Test2"); 
        if(arrivalTimes.size() < arrivalTimes2.size() && arrivalTimes.size() < arrivalTimes3.size() && arrivalTimes.size() < arrivalTimes4.size() && arrivalTimes.size() < arrivalTimes5.size()) 
        { 
         arrivalTimes.add(currentSecond); 
         System.out.println("Test"); 
        } 
        else if(arrivalTimes2.size() < arrivalTimes.size() && arrivalTimes2.size() < arrivalTimes3.size() && arrivalTimes2.size() < arrivalTimes4.size() && arrivalTimes2.size() < arrivalTimes5.size()) 
        { 
         arrivalTimes2.add(currentSecond); 
         System.out.println("Test"); 
        } 
        else if(arrivalTimes3.size() < arrivalTimes.size() && arrivalTimes3.size() < arrivalTimes2.size() && arrivalTimes3.size() < arrivalTimes4.size() && arrivalTimes3.size() < arrivalTimes5.size()) 
        { 
         arrivalTimes3.add(currentSecond); 
         System.out.println("Test"); 
        } 
        else if(arrivalTimes4.size() < arrivalTimes.size() && arrivalTimes4.size() < arrivalTimes3.size() && arrivalTimes4.size() < arrivalTimes2.size() && arrivalTimes4.size() < arrivalTimes5.size()) 
        { 
         arrivalTimes4.add(currentSecond); 
         System.out.println("Test"); 
        } 
        else{arrivalTimes5.add(currentSecond);} 
       } 

      } 

      // Check whether we can put the person into a line. 
      if ((!number.isBusy()) && (!arrivalTimes.isEmpty())) 
      { 
       next = arrivalTimes.remove(); 
       waitTimes.addNumber(currentSecond - next); 
       number.startMoving(); 
      } 
      if ((!number2.isBusy()) && (!arrivalTimes2.isEmpty())) 
      { 
       next = arrivalTimes2.remove(); 
       waitTimes2.addNumber(currentSecond - next); 
       number2.startMoving(); 
      } 
      if ((!number3.isBusy()) && (!arrivalTimes3.isEmpty())) 
      { 
       next = arrivalTimes3.remove(); 
       waitTimes3.addNumber(currentSecond - next); 
       number3.startMoving(); 
      } 
      if ((!number4.isBusy()) && (!arrivalTimes4.isEmpty())) 
      { 
       next = arrivalTimes4.remove(); 
       waitTimes4.addNumber(currentSecond - next); 
       number4.startMoving(); 
      } 
      if ((!number5.isBusy()) && (!arrivalTimes5.isEmpty())) 
      { 
       next = arrivalTimes5.remove(); 
       waitTimes5.addNumber(currentSecond - next); 
       number5.startMoving(); 
      } 


     // Subtract one second from the remaining time in the current li 
     number.reduceRemainingTime(); 
     number2.reduceRemainingTime(); 
     number3.reduceRemainingTime(); 
     number4.reduceRemainingTime(); 
     number5.reduceRemainingTime(); 
     } 




     // Write the summary information about the simulation. 
     System.out.println("\nCustomers served Line1: " + waitTimes.howManyNumbers()); 
     if (waitTimes.howManyNumbers() > 0) 
     System.out.println("Average wait Line1: " + waitTimes.average() + " sec"); 
     System.out.println("\nCustomers served Line2: " + waitTimes2.howManyNumbers()); 
     if (waitTimes.howManyNumbers() > 0) 
     System.out.println("Average wait Line2: " + waitTimes2.average() + " sec"); 
     System.out.println("\nCustomers served Line3: " + waitTimes3.howManyNumbers()); 
     if (waitTimes.howManyNumbers() > 0) 
     System.out.println("Average wait Line3: " + waitTimes3.average() + " sec"); 
     System.out.println("\nCustomers served Line4: " + waitTimes4.howManyNumbers()); 
     if (waitTimes.howManyNumbers() > 0) 
     System.out.println("Average wait Line4: " + waitTimes4.average() + " sec"); 
      System.out.println("\nCustomers served Line5: " + waitTimes5.howManyNumbers()); 
     if (waitTimes.howManyNumbers() > 0) 
     System.out.println("Average wait Line5: " + waitTimes5.average() + " sec"); 

} 
} 
+2

これは許可されています。あなたは「うまくいきません」を展開できますか? –

+0

はい............. –

+0

「動作しません」と定義します。おそらくあなたは既にそれがコンパイルされていることを確認していますが、それは合法Javaであることを意味します。 – EJP

答えて

2

はい、1缶鎖一緒に論理演算子の効果無数。

7

表現は完全に合法的な構文です。 &&演算子は短絡論理積であり、評価は左から右に進み、評価する最初の項(ある場合)は、falseになります。

EDITは、あなたの編集に基づいて

、私はこの問題は、ifの体は、numberのすべて場合にのみ、に入力されていることであると推測しています。 。 。number5がビジー状態です。 (Lineクラスがどのようなものか、まさにどのような振る舞いを見たいのか投稿していないためです)。メンテナンス可能)、QueueLineAveragerをカプセル化したクラスを定義した場合おそらく、これらの3つのオブジェクトが一緒になって、問題のドメインにとって意味のあるものを構成します。次に、これらのオブジェクトの配列(あるいはおそらくは、List)を宣言して、それらを独立変数として扱うのではなく、

0

はい、許可されています。 & &は短絡演算子なので、述語の1つが失敗すると、それ以上の評価は行われません。すべての条件を評価する場合は、&(つまり1つのアンパサンドのみ)を使用できます。すべての条件が評価されます。これを必要とする主な理由は、述部に発生させたい副作用がある場合です。 isBusy()のそれぞれが実行され、結果は戻り値のANDになります...

if (number.isBusy() & 
    number2.isBusy() & 
    number3.isBusy() & 
    number4.isBusy() & 
    number5.isBusy()) { 
} 

それは通常悪い習慣だが、それはあなたが欲しいものだようなあなたの場合にはそれが見えます。

if (number.isBusy() && 
    number2.isBusy() && 
    number3.isBusy() && 
    number4.isBusy() && 
    number5.isBusy()) { 
} 

1が失敗するまで各isBusy()は、次のisBusy()年代が呼び出されることはありません、実行されます。

0

user1247751は、より良い回答のために詳細情報を提供する必要があります。

しかし、提供された情報では、isBusyメソッドの1つがシステムの状態を変更して動作しないと推測できます。

関連する問題