java数组中的非重复随机数
我已经搜索过了,根本找不到我问题的答案。 我必须创建一个彩票类型的模拟,6个数字+没有重复的奖金数字。 我已经设法让它工作,但在6数字数组中,泊松[1]处的数字始终显示为0。我确信这是一个简单的问题,但我对这一点是全新的,因此任何帮助都将被感激地接受。代码如下,我确信它无处不在java数组中的非重复随机数,java,arrays,Java,Arrays,我已经搜索过了,根本找不到我问题的答案。 我必须创建一个彩票类型的模拟,6个数字+没有重复的奖金数字。 我已经设法让它工作,但在6数字数组中,泊松[1]处的数字始终显示为0。我确信这是一个简单的问题,但我对这一点是全新的,因此任何帮助都将被感激地接受。代码如下,我确信它无处不在 import java.util.Arrays; public class Lotto { public static void main(String[] args) { int[] Lo
import java.util.Arrays;
public class Lotto {
public static void main(String[] args) {
int[] Lotto=new int[6];
final int MIN = 1;
final int MAX = 45;
for (int i = 0; i< Lotto.length; ++i)
{
Lotto [0] =MIN+ (int)(Math.random()*((MAX -MIN)+1));
while (Lotto[1] == Lotto[0])
{
Lotto[1] =MIN+ (int)(Math.random()*((MAX -MIN)+1));
}
while ((Lotto[2] == Lotto[0]) || (Lotto[2] == Lotto[1]) )
{
Lotto[2] = MIN+ (int)(Math.random()*((MAX -MIN)+1));
while ((Lotto[3] == Lotto[0]) || (Lotto[3] == Lotto[1]) || (Lotto[3] == Lotto[2]) )
{
Lotto[3] = MIN+ (int)(Math.random()*((MAX -MIN)+1));
}
while ((Lotto[4] == Lotto[0]) || (Lotto[4] == Lotto[1]) || (Lotto[4] == Lotto[2]) || (Lotto[4] == Lotto[3]) )
{
Lotto[4] = MIN+ (int)(Math.random()*((MAX -MIN)+1));
while ((Lotto[5] == Lotto[0]) || (Lotto[5] == Lotto[1]) || (Lotto[5] == Lotto[2]) || (Lotto[5] == Lotto[3])|| (Lotto[5] == Lotto[4]))
{
Lotto[5] = MIN+ (int)(Math.random()*((MAX -MIN)+1));
}
int[] BonusNumber=new int[1];
for (int j = 0; j< BonusNumber.length; ++j)
{
BonusNumber [j] =1+ (int)(Math.random()*((45 -1)+1));
}
System.out.println("Winner " + Arrays.toString(Lotto));
System.out.println("Bonus Number" +Arrays.toString(BonusNumber));
{
}
}
}
}
}
}
导入java.util.array;
公共类彩票{
公共静态void main(字符串[]args){
整数[]乐透=新整数[6];
最终int最小值=1;
最终int最大值=45;
对于(int i=0;i
您可以使用既不保留顺序也不保留重复项的哈希集
您还可以在每次需要新的随机数时创建java.util.Random
类的新实例,因为这会更改种子并使应用程序具有更大的随机性,从而消除每次包含0的位置1
新建java.util.Random().nextInt(int max)
更改此循环:
while (Lotto[1] == Lotto[0])
{
Lotto[1] = random(MIN, MAX);
}
进入:
以及代码的其余部分。你看到问题出在哪里了吗
但在您开始重写非常复杂且不可维护的代码之前,请查看此实现,它更易于理解和。。。正确:
final int MIN = 1;
final int MAX = 45;
final List<Integer> allPossible = new ArrayList<Integer>(MAX - MIN + 1);
for (int i = MIN; i <= MAX; ++i) {
allPossible.add(i);
}
Collections.shuffle(allPossible);
final List<Integer> lotto = allPossible.subList(0, 6);
int bonusNumber = allPossible.get(6);
System.out.println("Winner " + lotto);
System.out.println("Bonus Number" + bonusNumber);
final int MIN=1;
最终int最大值=45;
最终列表所有可能=新阵列列表(最大-最小+1);
对于(int i=MIN;i我认为您的代码非常混乱,您可以通过使用一些API实用程序(如Collections.shuffle()
)避免太多情况,如本例所示:
public static void main(String[] args)
{
Integer[] numbers = new Integer[45];
// Populating numbers.
for (int i = 0; i < 45; i++)
numbers[i] = i + 1;
// Shuffling them to make them in random order.
List<Integer> list = Arrays.asList(numbers);
Collections.shuffle(list);
// Print any 6 of them. I chose first 6 ones for simplicity.
for (int i = 0; i < 6; i++)
System.out.print(list.get(i) + " ");
}
如果要使用int[]而不是内置类,可以这样做
int[] ints = new int[45];
for (int i = 0; i < ints.length; i++) ints[i] = i + 1;
Random rand = new Random();
for (int i = 0; i < 6; i++) {
int swap = rand.nextInt(ints.length - i) + i;
int tmp = ints[i];
ints[i] = ints[swap];
ints[swap] = tmp;
}
int[] drawn = Arrays.copyOf(ints, 6);
System.out.println(Arrays.toString(drawn));
我想我采用的是与您相同的intro Java类(因为该程序看起来非常熟悉),所以我会尽量将其保留在基本内容上;但是我确实从您的程序中复制了所有原始代码,所以您基本上完成了所有工作,我只是稍微整理了一下
这是完整的程序(将一组数字随机化(检查重复),允许用户选择一组或随机化自己的数字(也检查重复),对两组进行排序和比较,通知用户是否获胜,要求再次播放:
import java.util.*;
public class Lotto
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
boolean checkPlay = false, readInput = false;
final int MAX = 45;
while (readInput != true)
{
System.out.print("Would you like to buy a lotto ticket? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
checkPlay = true;
}
else
{
checkPlay = false;
}
readInput = true;
}
else
{
System.out.println("Invalid Input.");
}
}
while (checkPlay == true)
{
int[] lotto = new int[6];
int lottoLength = lotto.length;
int[] userLotto = new int[6];
int userLottoLength = userLotto.length;
for (int i = 0; i < lottoLength; i++)
{
boolean checkLotto = false;
while (checkLotto != true)
{
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (numCheck == lotto[j])
{
confirmSame = true;
}
}
if (confirmSame != true)
{
lotto[i] = numCheck;
checkLotto = true;
}
}
}
readInput = false;
while (readInput != true)
{
System.out.println("Would you like choose your own numbers or just randomize them?");
System.out.print("Choose your own numbers? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
for (int i = 0; i < userLottoLength; i++)
{
boolean checkUserLotto = false;
while (checkUserLotto != true)
{
System.out.print("Which number would you like to choose for number " + (i + 1) + ": ");
String numUserInput = keyboard.nextLine().trim();
int numUserInputLength = numUserInput.length();
boolean checkInput = true;
if (numUserInputLength > 2 || numUserInputLength < 1)
{
System.out.println("Invalid Input. Try again.");
checkInput = false;
}
else
{
for (int j = 0; j < numUserInputLength; j++)
{
if (Character.isDigit(numUserInput.charAt(j)) != true)
{
System.out.println("Invalid Input. Try again.");
checkInput = false;
}
}
}
if (checkInput == true)
{
int userInput = Integer.parseInt(numUserInput);
if (userInput > MAX || userInput < 1)
{
System.out.println("Invalid Input. Try again.");
}
else
{
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (userInput == userLotto[j])
{
System.out.println("You've already choosen this number. Choose again.");
confirmSame = true;
}
}
if (confirmSame != true)
{
userLotto[i] = userInput;
checkUserLotto = true;
}
}
}
}
}
}
else
{
for (int i = 0; i < userLottoLength; i++)
{
boolean checkLotto = false;
while (checkLotto != true)
{
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (numCheck == userLotto[j])
{
confirmSame = true;
}
}
if (confirmSame != true)
{
userLotto[i] = numCheck;
checkLotto = true;
}
}
}
}
readInput = true;
System.out.print("Your lotto numbers are: " + userLotto[0]);
for (int i = 1; i < userLottoLength; i++)
{
System.out.print(", " + userLotto[i]);
}
System.out.print("!");
System.out.println();
System.out.print("And the winning lotto numbers are: " + lotto[0]);
for (int i = 1; i < lottoLength; i++)
{
System.out.print(", " + lotto[i]);
}
System.out.print("!");
System.out.println();
Arrays.sort(lotto);
Arrays.sort(userLotto);
if (Arrays.equals(userLotto, lotto) == true)
{
System.out.println("Your lotto numbers match the winning lotto numbers! \nYou win!");
}
else
{
System.out.println("Your lotto numbers do not match the winning lotto numbers. \nYou lose.");
}
}
else
{
System.out.println("Invalid Input.");
}
}
readInput = false;
while (readInput != true)
{
System.out.print("Would you like to buy another lotto ticket? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
checkPlay = true;
}
else
{
checkPlay = false;
}
readInput = true;
}
else
{
System.out.println("Invalid Input.");
}
}
}
System.out.println("That's a good decision. Save your money");
System.exit(0);
}
}
如果numCheck
不等于任何输入的数字,则numCheck
将写入当前数组点。您有一个off-by-one错误,OP希望值从1
到45
,您将从0
返回到44
。没有问题。但是我建议您只需从1
到进行迭代@DelShekasteh..请不要要求别人对你的答案投赞成票。他们自己也会这样做,因为他们应该得到答案。只是一个建议而已。:)托马斯兹,非常感谢,我现在看到了。它可能无法维护,但我必须坚持我的课程文本,我们没有涵盖收藏,所以我不能使用它,似乎很愚蠢,但这是他们想要的方式,只有2个月,所以我确信我们会涵盖所有内容。
int[] ints = new int[45];
for (int i = 0; i < ints.length; i++) ints[i] = i + 1;
Random rand = new Random();
for (int i = 0; i < 6; i++) {
int swap = rand.nextInt(ints.length - i) + i;
int tmp = ints[i];
ints[i] = ints[swap];
ints[swap] = tmp;
}
int[] drawn = Arrays.copyOf(ints, 6);
System.out.println(Arrays.toString(drawn));
[19, 22, 24, 6, 34, 23]
import java.util.*;
public class Lotto
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
boolean checkPlay = false, readInput = false;
final int MAX = 45;
while (readInput != true)
{
System.out.print("Would you like to buy a lotto ticket? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
checkPlay = true;
}
else
{
checkPlay = false;
}
readInput = true;
}
else
{
System.out.println("Invalid Input.");
}
}
while (checkPlay == true)
{
int[] lotto = new int[6];
int lottoLength = lotto.length;
int[] userLotto = new int[6];
int userLottoLength = userLotto.length;
for (int i = 0; i < lottoLength; i++)
{
boolean checkLotto = false;
while (checkLotto != true)
{
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (numCheck == lotto[j])
{
confirmSame = true;
}
}
if (confirmSame != true)
{
lotto[i] = numCheck;
checkLotto = true;
}
}
}
readInput = false;
while (readInput != true)
{
System.out.println("Would you like choose your own numbers or just randomize them?");
System.out.print("Choose your own numbers? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
for (int i = 0; i < userLottoLength; i++)
{
boolean checkUserLotto = false;
while (checkUserLotto != true)
{
System.out.print("Which number would you like to choose for number " + (i + 1) + ": ");
String numUserInput = keyboard.nextLine().trim();
int numUserInputLength = numUserInput.length();
boolean checkInput = true;
if (numUserInputLength > 2 || numUserInputLength < 1)
{
System.out.println("Invalid Input. Try again.");
checkInput = false;
}
else
{
for (int j = 0; j < numUserInputLength; j++)
{
if (Character.isDigit(numUserInput.charAt(j)) != true)
{
System.out.println("Invalid Input. Try again.");
checkInput = false;
}
}
}
if (checkInput == true)
{
int userInput = Integer.parseInt(numUserInput);
if (userInput > MAX || userInput < 1)
{
System.out.println("Invalid Input. Try again.");
}
else
{
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (userInput == userLotto[j])
{
System.out.println("You've already choosen this number. Choose again.");
confirmSame = true;
}
}
if (confirmSame != true)
{
userLotto[i] = userInput;
checkUserLotto = true;
}
}
}
}
}
}
else
{
for (int i = 0; i < userLottoLength; i++)
{
boolean checkLotto = false;
while (checkLotto != true)
{
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (numCheck == userLotto[j])
{
confirmSame = true;
}
}
if (confirmSame != true)
{
userLotto[i] = numCheck;
checkLotto = true;
}
}
}
}
readInput = true;
System.out.print("Your lotto numbers are: " + userLotto[0]);
for (int i = 1; i < userLottoLength; i++)
{
System.out.print(", " + userLotto[i]);
}
System.out.print("!");
System.out.println();
System.out.print("And the winning lotto numbers are: " + lotto[0]);
for (int i = 1; i < lottoLength; i++)
{
System.out.print(", " + lotto[i]);
}
System.out.print("!");
System.out.println();
Arrays.sort(lotto);
Arrays.sort(userLotto);
if (Arrays.equals(userLotto, lotto) == true)
{
System.out.println("Your lotto numbers match the winning lotto numbers! \nYou win!");
}
else
{
System.out.println("Your lotto numbers do not match the winning lotto numbers. \nYou lose.");
}
}
else
{
System.out.println("Invalid Input.");
}
}
readInput = false;
while (readInput != true)
{
System.out.print("Would you like to buy another lotto ticket? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
checkPlay = true;
}
else
{
checkPlay = false;
}
readInput = true;
}
else
{
System.out.println("Invalid Input.");
}
}
}
System.out.println("That's a good decision. Save your money");
System.exit(0);
}
}
for (int i = 0; i < lottoLength; i++) {
boolean checkLotto = false;
while (checkLotto != true) {
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++) {
if (numCheck == lotto[j]) {
confirmSame = true;
}
}
if (confirmSame != true) {
lotto[i] = numCheck;
checkLotto = true;
}
}
}
for (int j = 0; j <= i; j++) {
if (numCheck == lotto[j]) {
confirmSame = true;
}
}