在检查数组的结果时,我一直在尝试寻找如何获取结果。
在我的数组中,我有三枚硬币,抛掷时,根据返回的随机数,结果是正面或反面。如果随机数小于 0.5,则为正面;如果大于 0.5,则为反面。
如果所有 3 次抛掷均 < 0.5,控制台应打印出“老杨线”。否则如果 >0.5 则为“老阴线”。
例如,如何检查循环以查找结果是否为 2 (<0.5) 和 1 (>0.5)?
这是我的代码:
public class BookOfChange {
public static void main(String[] args) {
Coin Coin1 = new Coin();
Coin Coin2 = new Coin();
Coin Coin3 = new Coin();
double firstThrow = Coin1.getFace();
double secondThrow = Coin2.getFace();
double thirdThrow = Coin3.getFace();
/**
* Test method
*/
System.out.println(Coin1.getFace());
System.out.println(Coin2.getFace());
System.out.println(Coin3.getFace());
double[] toss = {firstThrow,secondThrow,thirdThrow };
/**
* Test array
*/
System.out.println(toss);
for(int i = 0; i < toss.length; i++)
{
if(toss[i]<0.5) {
System.out.println(" Old Yang line");
}
else if(toss[i]>0.5) {
System.out.println(" Old Yin line ");
}
}
}
}
提前致谢
最佳答案
能不能直接检查第一、第二、第三次抛出的条件?看我的例子。
此外,您还调用 System.out.println(toss),要显示数组中的元素,您需要使用 Arrays.toString(ArrayName) 方法。
if(firstThrow < 0.5 && secondThrow < 0.5 && thirdThrow < 0.5) {
System.out.println("Old Yang line");
}
else {
System.out.println("Old Yin line");
}
关于java - 检查循环结果的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61494356/