3D彩票JAVA编程怎么写?
3个回答
展开全部
import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Scanner; public class Lotto { public static final int NUMBER = 7; public static void main(String[] args) { List<Integer> userNumbers = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); int i = 1; while(i < 8) { System.out.println("输入第" + i + "个数字: "); userNumbers.add(scanner.nextInt()); i ++; } System.out.println(prize(userNumbers.toArray(new Integer[NUMBER]))); } private static Integer[] generateLotto() { List<Integer> lottoNumbers = new ArrayList<Integer>(); Random rand = new Random(); int lottoNumber, i = 0; while(i < NUMBER) { lottoNumber = rand.nextInt(35) + 1; if(!lottoNumbers.contains(lottoNumber)) { lottoNumbers.add(lottoNumber); i ++; } } return bubbleSort(lottoNumbers.toArray(new Integer[NUMBER])); } private static Integer[] bubbleSort(Integer[] array) { for(int i = 0; i < array.length; i ++) { for(int j = 0; j < array.length - i - 1; j ++) { if(array[j] > array[j + 1]) { int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } return array; } public static String prize(Integer[] userNumbers) { int count = 0; Integer[] lottoNumbers = generateLotto(); for(Integer i : userNumbers) { for(Integer j : lottoNumbers) { if (i == j) { count ++; } } } return "猜对了 "+ count + " 个彩票号码"; }}
输入第1个数字:
5
输入第2个数字:
14
输入第3个数字:
19
输入第4个数字:
24
输入第5个数字:
33
输入第6个数字:
34
输入第7个数字:
27
猜对了 3 个彩票号码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询