编程实现35选7彩票销售程序。用户从键盘输入要买彩票的注数n,程序随机产生n注号码并输出。每注号码为7个1~35的随机整数,每注7个号码中不能有重复号码。
写了这么多写不来了T T最好能用简单点的 太难的看不懂呀xx
package ex3;
import java.util.Scanner;
class Lottey{
int[] a=new int[6];
static String zs;
public static boolean pd(int[] a,int num){
boolean flag = false;
for(int i=0;i<a.length;i++){
if(a[i] == num){
flag = true;
break;
}
}
return flag;
}
void generate(){
int num=0;
for (int i = 0; i < 7; i++) {
num=((int) (Math.random() * 35+1));
if (pd(a, num)) {
i=i-1;
}
else a[i]=num;
}
}
void pringTitle(){
System.out.println(" 彩票");
System.out.println("注数"+":"+zs);
}
}
public class LotteyText {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("输入注数");
Lottey.zs=input.next();
Lottey p =new Lottey();
Lottey.zs=input.next();
input.close();
}
}
public static void main(String[] args){
Vector v=new Vector();
for(int i=1;i<36;i++){
v.addElement(i);
}
int n=0;
for(int j=0;j<7;j++){
n=(int) (Math.random()*v.size());
System.out.println(v.elementAt(n));
v.removeElementAt(n);
}
}
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
public class CaiPiao {
@Test
public void test(){
for (int i = 0; i < 5; i++) {
numberSelect(7);
}
}
/**
* 生成一组号码
* @param count 一组的个数
*/
public void numberSelect(int count){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
createNumber(list);
}
System.out.println(StringUtils.join(list, ", "));
}
/**
* 防止重复
* @param list 号码容器
*/
public void createNumber(List<Integer> list){
int number = (int)(Math.random() * 35 + 1);
if(list.contains(number)){
createNumber(list);
}else{
list.add(number);
}
}
}
更改包名可以直接运行看效果,希望可以帮到你
package com.littlehow;
import java.util.*;
public class Lottery {
private static final int size = 7;
//可以在方法体中实例化
private static Random r = new Random();
private static List<Integer> getNumbers() {
//用于存放7个不同数字
Set<Integer> numbers = new HashSet<Integer>();
while (numbers.size() < size) {
numbers.add(r.nextInt(35) + 1);
}
List<Integer> returnValue = new ArrayList<Integer>(numbers);
returnValue.sort(null);//按从小到大排序
return returnValue;
}
public static void main(String[] args) {
System.out.print("请输入要购买的彩票数量: ");
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();//此处可能抛出非数字异常,可以进行处理
while (count-- > 0) {
System.out.println(getNumbers());
}
}
}