如何在同一行上打印随机数(例如:2,10,30,25,14)

时间:2016-11-28 14:54:37

标签: java eclipse loops for-loop

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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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();
    }
}