我創建了一個while循環,似乎無法正常運行。它會調用另一個類的方法來幫助更改我的程序的停止條件。它平均調用方法大約3-8次,永遠不會達到停止條件,但會停止。雖然循環沒有運行,直到會議完成條件
public class useExample
{
public static void main(String[] args)
{
Example ex = new Example();
long [] result;
long a = 0;
long b = 0;
long c = 0;
long d = 0;
long e = 0;
int count = 0;
int a1 = 1;
int b1 = 2;
int c1 = 3;
int d1 = 4;
int e1 = 5;
for(int i = 0; i <1; i++)
{
while(a != a1 && b != b1 && c != c1 && d != d1 && e != e1)
{
result = ex.getOnes();
a = result[0];
b = result[1];
c = result[2];
d = result[3];
e = result[4];
System.out.println(result[0] + " " + result[1] + " " + result[2] + " " + result[3] + " " + result[4]);
System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + count++);
}
System.out.println(a + " " + b + " " + c + " " + d + " " + e + " "+ count);
}
}
}
的實施例類是如下:
import java.util.*;
public class Example
{
Random r = new Random();
public long[] getOnes(){
int a = r.nextInt(35);
int b = r.nextInt(35);
int c = r.nextInt(35);
int d = r.nextInt(35);
int e = r.nextInt(35);
while(a == 0)
{
a = r.nextInt(35);
//temp[0] = a;
}
while(b == 0 || b == a /*|| b == c || b == d || b == e*/)
{
b = r.nextInt(35);
//temp[1] = b;
}
while(c == 0 || c == a || c == b /*|| c == d || c == e*/)
{
c = r.nextInt(35);
//temp[2] = c;
}
while(d == 0 || d == a || d == b || d == c/*|| d == e*/)
{
d = r.nextInt(35);
//temp[3] = d;
}
while(e == 0 || e == a || e == b || e == c|| e == d)
{
e = r.nextInt(35);
//temp[4] = e;
}
return new long[] {a, b, c, d, e};
}
}
的useExample類的while循環當同時的每個condtion是false.This應該只停止裝置時:
a == a1
b == b1
c == c1
d == d1
e == e1
它應該輸出它經過的循環數以及每個循環的值。最終輸出相同的a1值到e1。
*永遠不會達到停止條件,但它會停止*完全不清楚期望什麼? – Ravi
這段代碼沒有編譯,你有兩個名字爲e的變量在同一個範圍內。請發送有效的代碼 – CraigR8806
在每次調用getOnes()時,34 * 33 * 32 * 31 * 30有一次機會獲得正確的結果。你有足夠的時間嗎? –