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)
因此建立一個字符串並打印一次。 – epascarello