<java的彩票-百科-春风百科
> 百科 > 列表
java的彩票
时间:2024-12-23 18:19:10
答案

 import java.util.ArrayList;

 import java.util.Random;

 import java.util.Scanner;

 /**

 * 测试彩票类

 * @author doulmi

 *

 */

 public class LotteryDemo {

 public static void main( String[] args ) {

 Scanner scanner = new Scanner( System.in );

 System.out.print( "请输入要购买的注数:" );

 System.out.println();

 int buyN = 0;

 if ( scanner.hasNextInt() ) {

 buyN = scanner.nextInt();

 } else {

 System.out.println( "请输入正确的数字" );

 }

 for ( int i = 0; i < buyN; i ++ ) {

 Lottery lot = new Lottery();

 lot.generate();

 System.out.print( "第" );

 System.out.print( i );

 System.out.print( "注" );

 lot.printData();

 }

 }

 }

 /**

 * 彩票类

 * @author Administrator

 *

 */

 class Lottery {

 private static final int NUM_TO_GETOUT = 7;

 //能产生的最大数值为99

 private static final int MAX_NUM = 35;

 public ArrayList< Integer > list;

 private int[] result;

 public Lottery() {

 list = new ArrayList< Integer >();

 for ( int i = 1; i <= MAX_NUM; i ++ ) {

 list.add( i );

 }

 result = new int[ NUM_TO_GETOUT ];

 }

 public void generate() {

 Random r = new Random();

 for ( int i = 0; i < NUM_TO_GETOUT; i ++ ) {

 result[ i ] = list.remove( r.nextInt( list.size() ) );

 }

 }

 /**

 * 关于票头的打印。。我不知道票头是什么。

 */

 public void pringTitle() {

 }

 /**

 * 实现号码的打印

 */

 public void printData() {

 for ( int n : result ) {

 System.out.print( n );

 System.out.print( " " );

 }

 System.out.println();

 }

 }

推荐
© 2024 春风百科