多票Java抽奖计划

时间:2014-01-18 01:18:03

标签: java random numbers

如何制作一个循环,询问用户是否想要购买更多门票,以及如何让随机数生成器创建唯一数字。如果你可以帮助或向我展示网站,以了解我该如何做到这一点将是伟大的

谢谢

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

public class PowerBallm {
  //declaring the main method
  public static void main(String[] args) 
  {
    //prompt user to enter amount of ticket to buy between 1 and 5
    System.out.print("How many tickets would you like to purchase ? ");  
    Scanner amount = new Scanner (System.in);
    double ticket_amount;
    ticket_amount = amount.nextInt();
    while (ticket_amount > 5 || ticket_amount <= 0)
    {
      System.out.println("You can only purchase up to FIVE tickets, please try again");
      while (!amount.hasNextInt())
      {
        amount.next();
      }
      ticket_amount = amount.nextInt();
    }
    //creating an Array of 5 numbers
    int group1[] = new int[5];

    for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++)
    {
      for(int i = 0; i < group1.length; i++)
      {
        group1[i] = 1 + (int) (Math.random() * 56);
      }     

      //sort the elements
      //Arrays.sort(group1);

      //group2 number 
      int group2 = 1 + (int) (Math.random()*46);

      //print the ticket numbers numbers
      System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) + " your group2 ticket number is " + group2);      
    }
  }
}

2 个答案:

答案 0 :(得分:1)

除了检查无效的票数外,您的代码对输入的号码不做任何处理。您需要将代码放在for循环中创建和打印票证。

答案 1 :(得分:1)

您可以将代码从您生成故障单内容的位置包装到您在另一个循环中显示这些数字的位置。

for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++)
{
    for(int i = 0; i < group1.length; i++)
    {
        group1[i] = 1 + (int) (Math.random() * 56);
    }    

    //sort the elements
    Arrays.sort(group1);

    //group2 number 
    int group2 = 1 + (int) (Math.random()*46);

    //print the ticket numbers numbers
    System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) 
        + " your group2 ticket number is " + group2);
}