• 随机数猜1-9的数字


    这算是个小游戏 

    猜随机数

    代码如下

    package Day07; import java.lang.reflect.Array;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.Scanner; public class MatchingGame {
    public static void main(String[] args) {
    int[] arrOne = new int[5];
    for(int i = 0; i < arrOne.length; i++) {
    arrOne[i] = new Random().nextInt(10);
    for(int j = 0; j < i; j++) {
    if (arrOne[i] == arrOne[j]) {
    j = 0;
    arrOne[i] = new Random().nextInt(10);
    }
    }
    }
    int[] arrTwo = new int[5];
    Scanner sc = new Scanner(System.in);
    String str;
    // System.out.println(Arrays.toString(arrOne));
    boolean loopFlag = true;
    while (loopFlag) {
    for (int i = 0; i < arrOne.length; i++) {
    System.out.println("Please input arrTwo[" + i + "]");
    arrTwo[i] = sc.nextInt();
    }
    str = match(arrOne, arrTwo);
    System.out.println(str);
    loopFlag = !"a=5,b=0".equals(str);
    }
    }
    /**
    *
    * @param arrOne 源数组
    * @param arrTwo 与源数组匹配的数组
    * @return
    */
    public static String match(int[] arrOne, int[] arrTwo) {
    int a = 0;
    int b = 0;
    for (int i = 0; i < arrOne.length; i++) {
    for (int j = 0; j < arrTwo.length; j++) {
    if (i == j && arrOne[i] == arrTwo[j]) {
    a++;
    } else if (i != j && arrOne[i] == arrTwo[j]) {
    b++;
    }
    }
    }
    return "a=" + a + ",b=" + b;
    }
    }

    只相信苦尽甘来
  • 相关阅读:
    basic use of sidekiq
    查看远程git log
    通过rails console执行sql语句
    通过rails打开数据库
    如何用rake tasks 生成migration对应的sql
    Remainders Game (中国剩余定理)
    binary-tree-preorder-traversal
    minimum-depth-of-binary-tree (搜索)
    Find a multiple POJ
    linked-list-cycle (快慢指针判断是否有环)
  • 原文地址:https://www.cnblogs.com/F001li/p/7055912.html
Copyright © 2020-2023  润新知