检查循环结果的详细信息

时间:2020-04-29 04:40:32

标签: java arrays

在检查数组结果时,我一直试图寻找如何获得结果的方法。

在我的数组中,我有3个硬币,扔掉后,根据返回的随机数,结果是正面或反面。如果随机数小于0.5,则其头部;如果大于0.5,则其尾巴。

如果所有三掷都<0.5,则控制台应打印出“ Old Yang line”。否则,如果> 0.5,则为“旧阴线”。

作为示例,我如何检查循环以发现结果是两个(<0.5)和一个(> 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 ");
    }
    }
}
}

预先感谢

5 个答案:

答案 0 :(得分:0)

尝试一下:

    int count_line = 0;
    for(int i = 0; i < toss.length; i++)
    {
        if(toss[i]<0.5) {
            count_line = count_line+1;
            System.out.println(" Old Yang line");
        }
        else if (toss[i]>0.5) {
            System.out.println(" Old Yin line ");
        }
    }

    if (count_line == 2){
        //Your code here
    }

答案 1 :(得分:0)

这应该有帮助-

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();

    double[] toss = {firstThrow,secondThrow,thirdThrow  };
    int countYang = 0, countYin = 0;

    for(int i = 0; i < toss.length; i++) {
      if(toss[i]<0.5) {
        countYang++;
      } else {
        countYin++;
      }
    }

    if (countYang > countYin) {
         System.out.println(" Old Yang line");
    } else {
        System.out.println(" Old Yin line ");
    }
}

答案 2 :(得分:0)

尝试一下:

int headCount = 0;
        int tailCount = 0;
        for (int i = 0; i < toss.length; i++) {
            if (toss[i] < 0.5) {
                headCount++;
            } else if (toss[i] > 0.5) {
                tailCount++;
            }
        }
       if (headCount == 3)
        System.out.println(" Old Yang line");
       else if (tailCount == 3)
        System.out.println(" Old Yin line ");
       else if(headCount == 2 && tailCount ==1)
        System.out.println("Print ");

答案 3 :(得分:0)

这是另一种方式:

    int count_line = 0;
    for(int i = 0; i < toss.length; i++)
    {
        if(toss[i]<0.5) {
            count_line++;
        }
        if (count_line >= 2){
           System.out.println(" Old Yang line");
           break;
        }
    }
    if(count_line < 2)
    {
       System.out.println(" Old Yin line ");
    }

答案 4 :(得分:0)

您能不能直接检查第一,第二和第三掷的状况?看看我的例子。

还要调用System.out.println(toss),以显示Array中的元素,您需要使用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");
}
相关问题