如何更好地将我的用户输入法调用到我的主方法中,以检查获胜条件?

时间:2018-06-17 11:33:18

标签: java multidimensional-array input netbeans java.util.scanner

我很难在我的main方法中尝试使用我的calledNumsInput方法来检查胜利条件,其中用户输入与随机生成的2d数组中的值匹配。

现在我有一个占位符来显示我想要开始实现条件的位置,那就是在我的main方法结束时带有board [] []值的If语句。我是否只需要围绕一个calledNumsInput()创建大量的If语句。或者有没有办法将该方法实现到胜利条件。

我对编码很新,所以我不介意拉门逻辑。我鼓励它。

感谢您的时间。     包bingoWood;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class bingoWood {
static ArrayList<Integer> random=newArrayList<Integer>();
 private static Random r = new Random();
 public static void main(String[] args) {
    System.out.println("B  I  N  G  O");
    int[][] board = new int[5][5];        
    for(int i = 0; i < board.length; i++){
       for(int j =0; j <board.length; j++){
           if(j==0)
               board[i][j] = getUniqueRandom(15, 1);
           else if(j==1)
               board[i][j] = getUniqueRandom(30, 16);
           else if(j==2)
               board[i][j] = getUniqueRandom(45, 31);
           else if(j==3)
               board[i][j] = getUniqueRandom(60, 46);
           else if(j==4)
               board[i][j] =     getUniqueRandom (75, 61);               
         if(i==board.length/2 && j==board.length/2) board[i][j] = 0;
           System.out.printf("%-3s", board[i][j]);
       }
       System.out.println("");     
    }  
    if(board[0][0] == calledNumsInput() && board[1][1] == calledNumsInput()     //Diagonal TopLeft - BotRight Win condition
       && board[2][2] == calledNumsInput() && board[3][3] == calledNumsInput()
       && board[4][4] == calledNumsInput())                               
        System.out.println("BINGO");                                                    

    if(board[0][4] == calledNumsInput() && board[1][3] == calledNumsInput()     //Diagonal TopRight - BotLeft Win condition
       && board[2][2] == calledNumsInput() && board[3][1] == calledNumsInput()
       && board[4][0] == calledNumsInput()) 
        System.out.println("BINGO");

    if(board[4][4] == calledNumsInput() && board[3][3] == calledNumsInput()     
       && board[2][2] == calledNumsInput() && board[1][1] == calledNumsInput()
       && board[0][0] == calledNumsInput()) 
        System.out.println("BINGO");


}
static int getUniqueRandom(int max, int min){
    int num = r.nextInt(max-min+1) + min;
    while (random.contains(num)) {
        num = r.nextInt(max-min+1) + min;
    }
    random.add(num);
    return num;
  }
   static int calledNumsInput(){        
    Scanner input = new Scanner(System.in);

    System.out.print("Enter Called Number: ");
    int calledNum = input.nextInt();    

    return calledNum;
  }
}

2 个答案:

答案 0 :(得分:0)

这可能适用于获取那些If语句的位置,如果我可以在连续获得正确的5个输入之后结束它。

users/name/desktop

答案 1 :(得分:0)

据我所知,这里的问题是解决方案。我会坚持要你尝试理解代码,直到完全得到它为止。编程是一门艺术,你自己做得越多,你就会越发展。

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Solution {
static ArrayList<Integer> random=new ArrayList<Integer>();
private static Random r = new Random();
public static void main(String[] args) {
    System.out.println("B  I  N  G  O");
    int[][] board = new int[5][5];        
    for(int i = 0; i < board.length; i++){
       for(int j =0; j <board.length; j++){
           if(j==0)
               board[i][j] = getUniqueRandom(15, 1);
           else if(j==1)
               board[i][j] = getUniqueRandom(30, 16);
           else if(j==2)
               board[i][j] = getUniqueRandom(45, 31);
           else if(j==3)
               board[i][j] = getUniqueRandom(60, 46);
           else if(j==4)
               board[i][j] = getUniqueRandom (75, 61);               
         if(i==board.length/2 && j==board.length/2) board[i][j] = 0;
           System.out.printf("%-3s", board[i][j]);
       }
       System.out.println("");     
    }  
    boolean[][] myBingo = new boolean[5][5];
    while(true) {
        int calledNumsInput = calledNumsInput();
        for(int i = 0; i < board.length; i++){
            for(int j =0; j <board.length; j++){
                if(calledNumsInput == board[i][j]) {
                    System.out.println("B " + board[i][j] + " is on the Board!");   
                    myBingo[i][j] = true;
                }
            }
        }
        if(isBingo(myBingo)) {
            System.out.println("BINGO");break;
        }
    }
}
static boolean isBingo(boolean[][] myBingo) {
    int hcount = 0, vcount = 0, dcount = 0; 
    for(int i = 0; i < myBingo.length; i++){
        for(int j =0; j <myBingo.length; j++){
            if(myBingo[i][j] == true)
                hcount++; 
        }
        if(hcount == 5) return true;
        else hcount = 0; 
    }
    for(int i = 0; i < myBingo.length; i++){
        for(int j =0; j <myBingo.length; j++){
            if(myBingo[j][i] == true)
                vcount++; 
        }
        if(vcount == 5) return true;
        else vcount = 0; 
    }
    for(int i = 0; i < myBingo.length; i++){
        for(int j =0; j <myBingo.length; j++){
            if((i == j) && (myBingo[i][j] == true))
                dcount++; 
        }
        if((i == myBingo.length-1) && dcount == 5) return true;
        else if((i == myBingo.length-1)) dcount = 0; 
    }
    for(int i =0; i < myBingo.length; i++){
        for(int j =0; j < myBingo.length; j++){
            if((i+j == myBingo.length-1) && (myBingo[i][j] == true)) 
                dcount++;               
            }
        if((i == myBingo.length-1) && dcount == 5) return true;
        else if((i == myBingo.length-1)) dcount = 0; 
    }
    return false;
}
static int getUniqueRandom(int max, int min){
    int num = r.nextInt(max-min+1) + min;
    while (random.contains(num)) {
        num = r.nextInt(max-min+1) + min;
    }
    random.add(num);
    return num;
 }
 static int calledNumsInput(){        
        Scanner input = new Scanner(System.in);

    System.out.print("Enter Called Number: ");
    int calledNum = input.nextInt();    

    return calledNum;
  }
}