有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何让我的程序(彩票游戏)再次检查提示?

我正在尝试写一个彩票程序(基于匈牙利游戏,5个数字从1到90),它几乎可以正常工作,第一次我给它错误的数字(超过90或0),它告诉我我做错了。但第二次它没有。它继续用无效的数字执行游戏。你们有什么建议,你们会有什么不同的做法

谢谢你抽出时间

代码:

import java.util.*;

class Lottery {

static int hits = 0;
Integer[] tippek = new Integer[5];
Random rand = new Random();
ArrayList<Integer> nums = new ArrayList<Integer>();
static Lottery lot = new Lottery();

public static void main (String[] args){

    lot.wnums();
    lot.tipread();
    lot.tipcheck();
    lot.wincheck();
    lot.result();

}



public void wnums () {
        // GENERATING THE WINNER NUMBERS
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; set.size() < 5; i++){
            int x = rand.nextInt(90) + 1;
            set.add(x);
        }
        nums.addAll(set);

    }

public void tipread (){
    // READING THE TIPS
    System.out.println("Please write 5 different number from 1 to 90.");
    try{
    Scanner scan = new Scanner(System.in);
    tippek[0] = scan.nextInt();
    tippek[1] = scan.nextInt();
    tippek[2] = scan.nextInt();
    tippek[3] = scan.nextInt();
    tippek[4] = scan.nextInt();
    }catch (InputMismatchException ex){
        System.out.println("Error.");
    }
}

public void tipcheck() {

    int fault = 0;

    List<Integer> tips = Arrays.asList(tippek);
    try{
    for(int tipp : tippek){
        System.out.println(tipp);
        if(tipp == 0 || tipp > 90){
            fault++;
        }
    }
    if(fault == 1){
        System.out.println("One of your tips is invalid ");
        System.out.println("Write other numbers");
        lot.tipread();
    }
    if(fault > 1){
        System.out.println(fault + " of your tips are invalid ");
        System.out.println("Write other numbers");
        lot.tipread();
    }

    for(int tipp : tips){
        for(int i = 0; i < tips.size(); i++){
            if(tips.indexOf(tips.get(i)) != tips.lastIndexOf(tips.get(i))){
                System.out.println("You can write a number only once");
                lot.tipread();
            }
        }
    }

    }catch (NullPointerException ex){
        System.out.println("Error.");
    }



}

public void wincheck () {
    // CHECKING THE TIPS
    try{
    for(int tipp : tippek){
        for(int i = 0; i < 5; i++){
            if(nums.get(i) == tipp){
                hits++;
            }
        }
    }
    }catch(Exception ex){
        System.out.println(" ");
    }
}

public void result() {
    try{
    Arrays.sort(tippek);
    Collections.sort(nums);
    String tippeksor = Arrays.toString(tippek); 
    System.out.println("Your tips in ascending order: " + tippeksor);
    System.out.println("You have " + hits + " hits.");
    System.out.println("The winning numbers are: " + nums);

    }catch(Exception ex){
        lot.tipread();
    }

}
}

共 (0) 个答案