如何製作一個循環,詢問用戶是否想購買更多的門票,以及如何讓隨機數字生成器製作唯一的數字。如果你能幫助或告訴我的網站,發現我怎麼能做到這一點,將是巨大的多張門票Java彩票計劃
謝謝
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
public class PowerBallm {
//declaring the main method
public static void main(String[] args)
{
//prompt user to enter amount of ticket to buy between 1 and 5
System.out.print("How many tickets would you like to purchase ? ");
Scanner amount = new Scanner (System.in);
double ticket_amount;
ticket_amount = amount.nextInt();
while (ticket_amount > 5 || ticket_amount <= 0)
{
System.out.println("You can only purchase up to FIVE tickets, please try again");
while (!amount.hasNextInt())
{
amount.next();
}
ticket_amount = amount.nextInt();
}
//creating an Array of 5 numbers
int group1[] = new int[5];
for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++)
{
for(int i = 0; i < group1.length; i++)
{
group1[i] = 1 + (int) (Math.random() * 56);
}
//sort the elements
//Arrays.sort(group1);
//group2 number
int group2 = 1 + (int) (Math.random()*46);
//print the ticket numbers numbers
System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) + " your group2 ticket number is " + group2);
}
}
}
程序打算做什麼? – Christian
創建一個強力球程序,詢問用戶他們想要購買的票數,然後ramdonly爲票證生成5個不同的數字@Christian – user3208537
您確實需要專注於正確地設置代碼的格式。這非常難以閱讀,並且會阻止許多用戶甚至看到它,更不用說實際上在研究它。 – csmckelvey