JAVA每日一题01

本文介绍了一个使用Java编写的抽奖程序,该程序能够生成五组各包含六个不同数字(1-49之间)的抽奖号码组合。通过随机数生成及检查重复等步骤确保每组号码的独特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还是我来继续jythoner的JAVA每日一题吧!最近比较闲哦!希望jythone不要见怪哦!

真是不好意思啊!早上有点忙!呵呵!

 

题目:一项抽奖程序要求读者从整数1-49之间选择6个不同的数字。编写一个程序来完成这项工作,并生成5组结果。

 

package com.tengfei.lesson01;
public class Lottery {
  public static void main(String[]args) {
    int setCount = 5;      // Number of sets of lucky numbers.
    int setSize = 6;       // Number of lucky numbers in the set.
    int range = 49;        // Assume selecting integers between 1 and range.
    int lucky;             // Holds a lucky number candidate.
    int luckyCount;        // Holds count of lucky numbers in a set.

    for(int i = 0; i < setCount; i++) {
      int lucky1 = 0;                  // Lucky numbers for the set of 6.
      int lucky2 = 0;                   
      int lucky3 = 0;                   
      int lucky4 = 0;
      int lucky5 = 0;                   
      int lucky6 = 0;    

      luckyCount = 0;                   // Count of numbers found in the current set
      while(luckyCount < setSize) {
       // Generate a lucky number between 0 and 48 and add 1:
       lucky = (int)(range*Math.random()) + 1;
        switch(luckyCount) {
          case 0:                      // It is the first one 
            lucky1 = lucky;            // so just store it
            luckyCount++;              // and increment the count
            break;
          case 1:                      // For the second we must
            if(lucky != lucky1) {      // check that it is different from the first
              lucky2 = lucky;          // It is, so store it
              luckyCount++;            // and increment the count
            }
            break;
          case 2:                      // For the third we check aginst the previous two
            if(lucky != lucky1 && lucky != lucky2) {
              lucky3 = lucky;
              luckyCount++;
            }
            break;
           case 3:                     // Check against the previous three...
            if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3) {
              lucky4 = lucky;
              luckyCount++;
            }
            break;
           case 4:                     // Check against the previous four...
            if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3 && lucky != lucky4) {
              lucky5 = lucky;
              luckyCount++;
            }
            break;
           case 5:                    // Check against the previous five...
            if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3 && lucky != lucky4 && lucky != lucky5) {
              lucky6 = lucky;
              luckyCount++;
            }
            break;
        }
      }

      System.out.print("\nSet " + (i + 1) + ":");                        // Identify the set

      System.out.print(" " + lucky1 + " " + lucky2  + " " + lucky3  +    // and output the numbers
                       " " + lucky4  + " " + lucky5  + " " + lucky6);

       
      // If you want to be sure the numbers line up in columns you could use a
      // rather more complicated statement here instead of the above:
/*
      System.out.print((lucky1>9 ? " " :"  ") + lucky1 + 
                       (lucky2>9 ? " " :"  ") + lucky2 +
                       (lucky3>9 ? " " :"  ") + lucky3 +
                       (lucky4>9 ? " " :"  ") + lucky4 +
                       (lucky5>9 ? " " :"  ") + lucky5 +
                       (lucky6>9 ? " " :"  ") + lucky6);
*/
      // This makes use of the conditional operator to output an extra space
      // when a lucky number is a single digit.
    }
  }
}



 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值