JAVA每日一题01

还是我来继续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.
    }
  }
}



 

 

 

你可能感兴趣的:(java,c,haskell,Ruby,华为)