java - 检查循环结果的详细信息

标签 java arrays

在检查数组的结果时,我一直在尝试寻找如何获取结果。

在我的数组中,我有三枚硬币,抛掷时,根据返回的随机数,结果是正面或反面。如果随机数小于 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/

相关文章:

java - 谁能告诉我 read() 和 readline(); 之间有什么区别?

java - 在jsp方法中为div提供边框颜色的问题

pointers - 我可以使用指针在 Fortran 中创建子数组吗?

java - 无法制作正确的表格,GPA 计算无法返回正确的成绩

javascript - 如何检查传递给 Typescript 中函数的参数?

C语言循环打印数组中的字符串

java - Hibernate 中的正则表达式查询

eclipse - 取决于 Eclipse 中 tools.jar (Sun JDK) 的 com.sun.javadoc

java - 将 XML 文档转换为字符串

python - 在Python中将数组转换为字符串