简体   繁体   English

用户输入与数组值的比较

[英]User input comparison with values of array

I'm trying to make a lottery game in java to run in the console afterwards with user input.我正在尝试在 java 中制作一个彩票游戏,然后在用户输入的情况下在控制台中运行。 I have a [3][9] array of random numbers between 1-9 in column 1, 10-19 in column 2, until 90, with half the numbers being 0, meaning they aren't part of the game or simply blanks.我有一个 [3][9] 随机数数组,第 1 列中的 1-9,第 2 列中的 10-19,直到 90,其中一半的数字为 0,这意味着它们不是游戏的一部分或只是空白. So far, I have the numbers created in the array and they output fine, but I need to allow the user to have input and the numbers being guessed to start as blanks (or x instead of the number) and when the user actually gets the right number, it would switch that with the number generated previously.到目前为止,我已经在数组中创建了数字,它们 output 很好,但是我需要允许用户输入并且猜测的数字以空格(或 x 而不是数字)开头,并且当用户实际获得正确的数字,它将与之前生成的数字切换。 This would repeat itself until all the numbers were right, and then a message indicating a win would show.这将不断重复,直到所有数字都正确为止,然后会显示一条表明获胜的消息。

How can I compare the inputs with the values generated?如何将输入与生成的值进行比较? And how do I hide these values until they are guessed by the user?以及如何隐藏这些值,直到用户猜到它们?

Final Edit: If a line is completed with correct numbers, how do I keep track of this to also display message if this happens?最终编辑:如果一行以正确的数字完成,如果发生这种情况,我如何跟踪这一点以显示消息?

This is the random array index:这是随机数组索引:

import java.util.Arrays;
import java.util.Random;

class Loto {

public static void main(String[] args) {
    int[][] cartao = new int[3][9];
    
    Random rand = new Random();
    for(int i = 0; i< cartao.length; i++){
        for(int j = 0; j< 5; j++){
            int x = rand.nextInt(89) + 1;
            while(cartao[i][x / 10] !=0) {
                x = rand.nextInt(89) + 1;
            }  
            cartao[i][x / 10] = x;
        }
    }
    
    for(int[] row : cartao){
        System.out.println(Arrays.toString(row));
    }        
}
}

You have to store what has been guessed correctly or not.您必须存储已正确或未正确猜测的内容。 For example you can use an auxiliary boolean matrix though it is not necessary (u can use only your card array, storing correctly guessed guesses has -1 for example), but it is easier to the eye I would say例如,您可以使用辅助 boolean 矩阵,尽管这不是必需的(您只能使用您的卡片数组,例如存储正确猜测的猜测有 -1),但我会说它更容易看

public class Lotto {
boolean correctlyGuessed[][];
int lottoCard[][];
public Lotto() {
    correctlyGuessed = new boolean[3][9];
    lottoCard = new int[3][9];
    
    Random rand = new Random();
    for(int i = 0; i< lottoCard.length; i++){
        for(int j = 0; j< 5; j++){
            int x = rand.nextInt(89) + 1;
            while(lottoCard[i][x / 10] !=0) {
                x = rand.nextInt(89) + 1;
            }  
            lottoCard[i][x / 10] = x;
        }
    }
}

public boolean guess(int row, int col, int number) {
    if(lottoCard[row][col] == number) {
        correctlyGuessed[row][col] = true;
        return true;
    }
    return false;
}

public boolean hasLottoEnded() {
    for(boolean[] arr:correctlyGuessed) {
        for(boolean guess: arr) {
            if(!guess) //if a guess is still false the game hasn't ended
                return false;
        }
    }
    return true;
}

public int getNumber(int row, int col) {
    return lottoCard[row][col];
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    for(int row = 0; row < lottoCard.length; row++) {
        for(int col = 0; col < lottoCard[row].length; col++) {
            if(correctlyGuessed[row][col])
                sb.append(lottoCard[row][col] + " ");
            else
                sb.append("X ");

        }
        //spacing
        sb.append("\n");
    }
    return sb.toString();
}

public static void main(String[] args) {
    Lotto card = new Lotto(); 
    
    Scanner sc = new Scanner(System.in);
    while(!card.hasLottoEnded()) { //loop whilst the game hasn't ended
        System.out.println("Row:");
        int row = sc.nextInt();
        System.out.println("Column:");
        int col = sc.nextInt();
        System.out.println("Number:");
        int number = sc.nextInt();
        if(card.guess(row, col, number))
            System.out.println("You have guessed correctly!");
        else
            System.out.println("Wrong guess :(");
        System.out.println(card);
    }
    System.out.println("You win!");
    sc.close();
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM