【问题标题】:How do I get my program (Lottery game) to check the tips again?如何让我的程序(彩票游戏)再次检查提示?
【发布时间】:2019-04-20 03:32:20
【问题描述】:

我正在尝试编写一个彩票程序(基于匈牙利游戏,从 1 到 90 的 5 个数字),它几乎可以正常工作,第一次给它错误的数字(超过 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();
    }

}
}

【问题讨论】:

  • tipcheck 在无效号码上调用tipread,但不会再次tipcheck。解决方案:如果一切正常,让tipcheck 返回一个布尔值。使用 while 循环读取,然后检查直到 tipcheck 满意。

标签: java arrays methods return


【解决方案1】:

包含lot.tipcheck() 作为tipread() 中的最后一条语句:

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();

    lot.tipcheck();
    }catch (InputMismatchException ex){
        System.out.println("Error.");
    }
}

这样才能确保每次用户输入有效数字时都会对其进行检查。
其他修改:
main() 中删除lot.tipcheck();
tipcheck() 中的每个lot.tipread(); 之后添加一个return;

【讨论】:

    【解决方案2】:

    而不是直接从tipcheck 调用tipread tipcheck 返回 true 如果一切正常,false 否则 并在 main 中使用 do-while

    public static void main (String[] args){
       lot.wnums();
       do {
         lot.tipread();
        } while(!lot.tipcheck());
       lot.wincheck();
      lot.result();
    }
    

    public boolean tipcheck() {
    int fault = 0;
    
    List<Integer> tips = Arrays.asList(tippek);
    try{
      for(int tipp : tippek){
        System.out.println(tipp);
        if(tipp < 1 || tipp > 90){
          fault++;
        }
      }
      if(fault == 1){
        System.out.println("One of your tips is invalid ");
        System.out.println("Write other numbers");
        // lot.tipread();
        return false;
      }
      if(fault > 1){
        System.out.println(fault + " of your tips are invalid ");
        System.out.println("Write other numbers");
        // lot.tipread();
        return false;
      }
    
      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();
            return false;
          }
        }
      }
    
    }catch (NullPointerException ex){
      System.out.println("Error.");
      return false;
    }
    return true;
    }
    

    像这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多