这是我的代码
import java.util.Scanner; //make use of scanner class to input values
import java.util.Random; //get random number
public class MarkSix5
{
public static void main(String[] args)
{
final int SIZE_OF_MARKSIX = 7;
final int SIZE_OF_PERIODS = 5;
//Ask user to enter how many periods of draw results he/she wants
Scanner input = new Scanner(System.in);
int[] markSix = new int[SIZE_OF_MARKSIX * SIZE_OF_PERIODS];
//Ask user to enter the past results
for(int j=0; j<5; j++)
{
int temp=0;
String periodNumber="";
if (j==0) periodNumber="1st";
else if (j==1){periodNumber="2nd";temp =7;}
else if (j==2){periodNumber="3rd";temp=14;}
else if (j==3){periodNumber="4th";temp=21;}
else {periodNumber="5th";temp=28;}
System.out.print("Please enter the past results of the " + periodNumber + " MarkSix: ");
for(int i=1*temp; i<SIZE_OF_MARKSIX+temp; i++)
{
markSix[i]=input.nextInt();
}
System.out.println("");
}
//generate random numbers
Random rand = new Random();
int[] theRandomNumber = new int[6];
int decision=1;
do
{
//initialize the new set of numbers
for(int k =0; k<6; k++)
{
theRandomNumber[k] = rand.nextInt(49)+1;
}
//check that the new set of numbers with conditions
for(int a=0;a<7;a++)
{ //need to consider how many times to check
for(int k=0;k<6;k++)
{
for(int j=0; j<35;j++) //ensure no past draw numbers
{
while (theRandomNumber[k]==markSix[j])//**This is the bug**
{
theRandomNumber[k] = rand.nextInt(49)+1;
}
}
{ for(int j=0; j<6;j++) //check duplicate numbers
{
while (theRandomNumber[k]==theRandomNumber[j])//**This is the bug**
{
theRandomNumber[k] = rand.nextInt(49)+1;
}
}
}
}
}
//print out results
System.out.println("A new set of numbers: " +theRandomNumber[0] + " " +theRandomNumber[1] + " "+theRandomNumber[2]+" "+ theRandomNumber[3]+ " " +theRandomNumber[4]+ " "+theRandomNumber[5]+".");
System.out.println("Do you want to have another set of numbers?(if yes, type 0)");
decision = input.nextInt();
}while (decision==0);
}
}
程序应首先要求用户输入5组数字。 然后打印出一组在5组数字中不存在的新数字。
在我改变之后,它确实有效。但我无法弄清楚为什么while循环不起作用。
答案 0 :(得分:0)
我不确定你为什么要做这个循环,但尝试更简单,例如:
final int SIZE_OF_MARKSIX = 7;
final int SIZE_OF_PERIODS = 5;
int decision = 0;
Scanner input = new Scanner(System.in);
do
{
Set<Integer> ints = new HashSet<>();
for (int j = 0; j < SIZE_OF_PERIODS; j++) {
String periodNumber = "";
if (j == 0)
periodNumber = "1st";
else if (j == 1) {
periodNumber = "2nd";
} else if (j == 2) {
periodNumber = "3rd";
} else if (j == 3) {
periodNumber = "4th";
} else {
periodNumber = "5th";
}
System.out.print("Please enter the past results of the " + periodNumber + " MarkSix: ");
for (int i = 0; i < SIZE_OF_MARKSIX ; i++) {
ints.add(input.nextInt());
}
System.out.println("");
}
Random rand = new Random();
List<Integer> randomInts = new ArrayList<>();
while (randomInts.size() < 6)
{
int nextInt = rand.nextInt(49);
if(!ints.contains(nextInt))
randomInts.add(nextInt);
}
System.out.println(randomInts);
System.out.println("Do you want to have another set of numbers?(if yes, type 0)");
decision = input.nextInt();
}while (decision==0);
input.close();
我放了一组整数,这会保存你的输入,如果有人被重复,它就不会被保存。
我将一个整数列表作为结果,并随机编号。如果随机是在集合中,我跳过这个数字到下一个。如果我不接受这是一个很好的价值。
重复此操作,直到您有6个新号码。
最后记得关闭扫描仪。
你没有发现错误,好像有人写了字母而不是数字..小心。
此致
答案 1 :(得分:0)
你输入带有k = j的while循环,所以你必须检查这不会发生,例如:
if(k!=j){
while (theRandomNumber[k]==theRandomNumber[j]) {
theRandomNumber[k] = rand.nextInt(49)+1;
}
}