2016-11-28 52 views
-1
import java.util.Random; 
import java.util.Scanner; 

public class LuckyForLife { 

    public static int randomNum(int min, int max){ 

    Random lotterynum = new Random(); 
    int randomNum = lotterynum.nextInt((max-min)+1)+min; 
    return randomNum; 


    } 




    //********************************************** 

    private static boolean doThisAgain(Scanner sc, String prompt) { 
     System.out.print(prompt); 
     String doOver = sc.nextLine(); 
     return doOver.equalsIgnoreCase("Y"); 
    } 

    //********************************************** 

    public static void main(String args[]) { 
     final String TITLE = "Lucky For Life Lottery"; 
     final String CONTINUE_PROMPT = "Do this again? [y/N] "; 

     System.out.println("Welcome to " + TITLE); 
     Scanner sc = new Scanner(System.in); 
     do { 
      for(int counter=1; counter<=5; counter++) { 
       System.out.printf("Your Lucky For Life Cash numbers are: " + randomNum(1,43)); 
       System.out.println(" Your Lucky Ball number is: " + randomNum(1,43)); 
      } 
     } while (doThisAgain(sc, CONTINUE_PROMPT)); 
     sc.close(); 
     System.out.println("Thank you for using " + TITLE); 
    } 

} 

我想獲得所有的隨機值在一行上打印,所以它看起來像像列表而不是程序執行5次。 (例如:你的數值是5,10,15,34,41)我怎樣才能得到隨機數打印在同一行(例如:2,10,30,25,14)

+0

因此建立一個字符串並打印一次。 – epascarello

回答

0

你可以使用一個數組來存儲的隨機數,然後使用StringBuilder構建最後一句:

public static void main(final String args[]) { 

    final String TITLE = "Lucky For Life Lottery"; 
    final String CONTINUE_PROMPT = "Do this again? [y/N] "; 

    System.out.println("Welcome to " + TITLE); 
    Scanner sc = new Scanner(System.in); 

    int totalNumbers = 5; 
    int[] numbers = new int[totalNumbers]; 

    do { 

     for (int counter = 0; counter < totalNumbers; counter++) { 

      numbers[counter] = randomNum(1, 43); 

     } 

     StringBuilder builder = new StringBuilder(); 

     builder.append("Your Lucky For Life Cash numbers are: "); 

     for (int number : numbers) { 

      builder.append(number); 
      builder.append(' '); 
     } 

     System.out.println(builder); 
     System.out.println("Your Lucky Ball number is: " + randomNum(1, 43)); 

    } while (doThisAgain(sc, CONTINUE_PROMPT)); 

    sc.close(); 
    System.out.println("Thank you for using " + TITLE); 
} 
+0

非常感謝你,完美的作品!我也看到你在那裏做了什麼! – GCS1249

0

我稍微改了一下你的代碼,看看這是否適合你的需求。

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

public class LuckyForLife { 
    private static final String TITLE = "Lucky For Life Lottery"; 
    private static final int MIN_RANDOM_INT = 1; 
    private static final int MAX_RANDOM_INT = 43; 

    public static void main(String[] args) { 
     System.out.println("Welcome to "+ TITLE); 
     String continueAnwser = "Y"; 
     Scanner inputUser = new Scanner(System.in); 
     try { 
      while ("Y".equalsIgnoreCase(continueAnwser)) { 
       System.out.println("How many number do you wish to generate?"); 
       int numberOfElementsTogenerate = inputUser.nextInt(); 
       String randomNumbers = generateRandomNumbersAsString(numberOfElementsTogenerate); 
       System.out.println("Your Lucky Ball number is:"+ randomNumbers); 
       System.out.println("Do this again? [y/N]:"); 
       continueAnwser = inputUser.next(); 
      } 
      System.out.println("Thank you for using " + TITLE); 
     } finally { 
      if (inputUser != null) { 
       inputUser.close(); 
      } 
     } 
    } 

    private static String generateRandomNumbersAsString(int numberOfElementsTogenerate) { 
     Random random = new Random(); 
     StringBuilder numersGeneratedBuider = new StringBuilder(); 
     numersGeneratedBuider.append("["); 
     for (int i = 0; i < numberOfElementsTogenerate; i++) { 
      numersGeneratedBuider.append(random.nextInt((MAX_RANDOM_INT - MIN_RANDOM_INT) + 1) + MIN_RANDOM_INT); 
      if (i < numberOfElementsTogenerate-1) { 
       numersGeneratedBuider.append(","); 
      } 
     } 
     numersGeneratedBuider.append("]"); 
     return numersGeneratedBuider.toString(); 
    } 
}