比较两个4位整数,逐位数

时间:2014-01-24 02:40:41

标签: java arrays parsing int compare

我正在编写一个正在写的程序。它应该是一种猜谜游戏。 该程序将创建一个随机的4位数字,其数字不会在任何地方重复。 然后将要求用户输入一个4位数字。然后程序会将这个数字与它所做的随机数字进行比较。对于任何相同且正确位置的数字,它将使变量(简单地说A)增加1(1A,2A,3A ......)。对于每个正确但位置错误的数字,它将以相同的方式增加另一个变量(比如说B)。在每次错误答案之后打印之前,用户将尝试使用两个变量进行正确猜测。

我决定将这两个数字存储在一个int数组中并尝试按照这种方式进行比较(沿着数组的每个元素移动并检查匹配),现在我无法将2个数字转换为数组。一个人打印出[1,2,3,4]而用户猜出打印出来[1 2 3 4]我不确定这是否有所作为,但我似乎无法弄清楚原因。

此外,我不确定这是否是比较两个数字的最简单方法,我也不知道如何将它们放入数组中。也许如果这样的陈述?一些指导会非常有用。

    int x = 0;
    int y = 0;
    if (random[0] == guess[0]){
    x+1; }
    else if (random[0] == guess[1] || random[0] == guess[2] || random[0] == guess[3])
    y+1;

这是我到目前为止所做的,我试图从用户分割字符串并将其存储为int但它说“无法在原始类型int上调用intAt(int)”

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

public class Game {

    public static void main(String[] args) {

        // create scanner object
        Scanner keyboard = new Scanner(System.in);

        // give user the game rules
        System.out.println("The computer has chosen a unique 4 digit number (No digits in the number repeat)\n"
                        + "Each time you guess incorectly you will get a hint. You must guess the number correctly in 5 tries or fewer to win.\n");

        // ask user for their guess
        System.out.println("Please enter your guess: ");

        // read the user input and store it in a string
        int input = keyboard.nextInt();

        int[] guess1 = new int[4];

        for (int i = 0; i < 4; i++) {
            guess1[i] = Integer.parseInt(String.valueOf(input.intAt(i)));
        }

        System.out.println(Arrays.toString(guess1));

    }

    public static int[] numberGenerator(int[] args) {

        // random number generator
        Random randy = new Random();

        // integer array with 4 positions (for each digit)
        int[] randArray = new int[4];

        // creates a variable for each position in the array
        int rand0 = 0;
        int rand1 = 0;
        int rand2 = 0;
        int rand3 = 0;

        // assigns a random value to temporary variable
        int a = randy.nextInt(9);
        int b = randy.nextInt(9);
        int c = randy.nextInt(9);
        int d = randy.nextInt(9);

        // tests to make sure there are no repeating digits in the number
        rand0 = a;

        if (b == a) {
            b = randy.nextInt(9);
        } else
            rand1 = b;

        if (c == a || c == b) {
            c = randy.nextInt(9);
        } else
            rand2 = c;

        if (d == a || d == b || d == c) {
            d = randy.nextInt(9);
        } else
            rand3 = d;

        // assigns random integers to their location in the array
        randArray[0] = rand0;
        randArray[1] = rand1;
        randArray[2] = rand2;
        randArray[3] = rand3;

        // prints and returns the array with its values
        System.out.println(Arrays.toString(randArray));
        return randArray;

    }

    // public static String[] compareGuess(String[] args, String str) {

    // return guess1;
    // }

}

同样,任何有关更简单方法的指导都会受到赞赏(我是新的,如果可以的话,尽量保持基本的java)

2 个答案:

答案 0 :(得分:1)

要修复上述代码,请使用String代替int

String input = keyboard.nextLine();

然后在for循环:

for (int i = 0; i < 4; i++) {
    guess1[i] = Integer.parseInt(String.valueOf(input.charAt(i)));
}

答案 1 :(得分:0)

您的代码无法处理重复数字生成两次的情况。

numberGenerator不需要输入参数,可以大大简化

从控制台读取字符串将简化操作。

下面的代码应该足以让你克服这个障碍

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

public class Game {

    public static void main(String[] args) {

        // create scanner object
        Scanner keyboard = new Scanner(System.in);

        // give user the game rules
        System.out.println("The computer has chosen a unique 4 digit number (No digits in the number repeat)\n"
                        + "Each time you guess incorectly you will get a hint. You must guess the number correctly in 5 tries or fewer to win.\n");

        int[] solution = numberGenerator();
        // ask user for their guess
        System.out.println("Please enter your guess: ");

        // read the user input and store it in a string
        String input = keyboard.nextLine();

        int[] guess1 = new int[4];

        for (int i = 0; i < 4; i++) {
            guess1[i] = Integer.parseInt(String.valueOf(input.charAt(i)));
        }

        // this is some rough comparison code - you'll need to work on this

        if(solution[0] == guess1[0])
        {
            System.out.println("The first digit was correct!");
        }
        if(solution[1] == guess1[1])
        {
            System.out.println("The second digit was correct!");
        }
        if(solution[2] == guess1[2])
        {
            System.out.println("The third digit was correct!");
        }
        if(solution[3] == guess1[3])
        {
            System.out.println("The fourth digit was correct!");
        }

        System.out.println(Arrays.toString(guess1));

    }

    public static int[] numberGenerator() {

        // random number generator
        Random randy = new Random();

        // integer array with 4 positions (for each digit)
        int[] randArray = new int[4];

        // assigns a random value to temporary variable
        int a = randy.nextInt(9);
        int b = randy.nextInt(9);
        int c = randy.nextInt(9);
        int d = randy.nextInt(9);

        // tests to make sure there are no repeating digits in the number

        while (b == a) {
            b = randy.nextInt(9);
        }

        while (c == a || c == b) {
            c = randy.nextInt(9);
        }

        while (d == a || d == b || d == c) {
            d = randy.nextInt(9);
        }

        // assigns random integers to their location in the array
        randArray[0] = a;
        randArray[1] = b;
        randArray[2] = c;
        randArray[3] = d;

        // prints and returns the array with its values
        System.out.println(Arrays.toString(randArray));
        return randArray;
    }
}