按列匹配数组编号

时间:2016-11-17 10:32:56

标签: java

我被要求实施彩票计划。代码如下..

Scanner keyIn = new Scanner(System.in);
Random randomGenerator = new Random();

int numDigits = 6, randomNum;
int[] userNum = new int[numDigits];
int[] lotteryNumbers = new int[numDigits];
for (int i = 0; i < 6; i++) {
    randomNum = (int) (Math.random() * 45);
    for (int x = 0; x < i; x++) {
        if (lotteryNumbers[x] == randomNum || lotteryNumbers[x] == 0) {
            randomNum = (int) (Math.random() * 45);
            x = -1;
        }
    }
    lotteryNumbers[i] = randomNum;
}

for (int i = 0; i < userNum.length; i++) {
    System.out.print("Enter your numbers: ");
    userNum[i] = keyIn.nextInt();
    System.out.println("Your number is: " + userNum[i]);
    System.out.println(" ");
}
keyIn.close();

System.out.println("You've entered the following numbers...");
System.out.println(Arrays.toString(userNum));

System.out.println(" ");
System.out.println("The lottery numbers are: ");
for (int i = 0; i < 6; i++) {
    Arrays.sort(lotteryNumbers);
    System.out.print(+lotteryNumbers[i] + " ");
}

Revision Question 9

我正在尝试实现问题的匹配部分,但我遇到了一些问题,它要求我在列而不是行中查找匹配。
我需要知道如何检查列中的数字是否匹配 我的尝试。

for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++)
{
if(lotteryNumbers[i] == userNum[j])
{
match++;
}
}
}
System.out.println(" ");
System.out.print("Matches: " +match +" found.");  

但是这会在行中查找匹配项,我想这样做,以便按列

查找匹配项

4 个答案:

答案 0 :(得分:1)

   for(int i=0;i<6;i++){
        for(int j=i;j<6;j++){
            if(userNum[i]==lotterNumbers[j]){
                System.out.println("match found: "+userNum[i]);
            }
        }
    }

此代码查看您是否在用户输入和随机数中匹配数字,如果是,则打印匹配找到+数字

数组为行:1|2|3|4|5|6|7

数组为列:

1

2

3

4

5

6

如果你看一个像行一样的数组你按列匹配,否则按行

答案 1 :(得分:0)

您可以做的最好的事情是使用HashMap

首先插入<column number, value in that column>作为第1行。

然后使用get()获取存储在该列中的值。然后只需使用get()中的内容检查第二行值。

代码:

import java.util.HashMap;
import java.util.Map;

public class Main
{
    public static void main(String[] args)
    {
        int matched = 0,num;

        Map<Integer,Integer> hm = new HashMap<Integer,Integer>();
        int[] numbersDrawn = {1,3,4,17,23,45};
        int[] yourNumbers = {1,2,3,19,23,41};

        for(int i = 0; i<numbersDrawn.length ; i++)
        {
            hm.put(i,numbersDrawn[i]);
        }

        for(int i=0 ; i<yourNumbers.length ; i++)
        {
            num = hm.get(i);

            if(num == yourNumbers[i])
            {
                matched++;
            }
        }
        System.out.println(matched);
    }
}

答案 2 :(得分:0)

 List<Integer> list = IntStream.range(1, 5001).boxed().collect(Collectors.toList());

Collections.shuffle(list);

 System.out.println(list.subList(0, 6));

答案 3 :(得分:0)

不要相信用户。他们可以输入重复的条目。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;


public class Test {

    public static void main(String args[]) {

        Scanner keyIn = new Scanner(System.in);
        Random randomGenerator = new Random();

        int numDigits = 6, randomNum;
        TreeSet<Integer> usernum = new TreeSet<>();
        int[] lotteryNumbers = new int[numDigits];
        ArrayList<Integer> matches = new ArrayList<>();

        for(int i = 0;i < 6; i++){
            randomNum = (int)(Math.random() * 45);
            for(int x = 0; x < i; x++){
                if(lotteryNumbers[x] == randomNum || lotteryNumbers[x] == 0)
                {
                    randomNum = (int)(Math.random() * 45);
                    x =-1;
                }
            }
            lotteryNumbers[i] = randomNum;
        }

        Arrays.sort(lotteryNumbers);

        System.out.println("Enter your numbers: ");
        for(int i = 0; i < numDigits; i++){
            boolean wasAdded = usernum.add(keyIn.nextInt());
            if(wasAdded == false) {
                System.out.println("Duplicate entries are disallowed!");
                i--;
            }
        }

        keyIn.close();

        System.out.print("You've entered the following numbers : ");
        System.out.println(usernum.toString().replaceAll("\\[|\\]|,", ""));

        System.out.println();
        System.out.print("The lottery numbers are: ");

        for(int i = 0; i < 6; i++){
            System.out.print(lotteryNumbers[i] +" ");
        }

        int iteration = 0;
        for (int entry : usernum) {
            if(entry == lotteryNumbers[iteration]) {
                matches.add(entry);
            }
            iteration++;
        }

        System.out.println("\n");
        System.out.println("Numbers matched : " + matches.size());

    }

}

enter image description here