java - 使用数组存储多个用户生成的整数

标签 java arrays

Description of Example

我是 Java 的新手,我正在尝试了解如何执行此示例。我最初计划将 3 个用户猜测存储到一个名为 UserGuess 的数组中,并将 3 个尝试猜测存储在另一个数组中,但我对如何将用户输入存储到一个数组中感到困惑(我只使用固定数字) .我也很困惑,如果 UserGuess 数组的整数等于尝试猜测的整数,我将如何写出,将出现一个消息框,指示锁已打开或尝试猜测是错误的。

package ComboLockerDrive;

导入javax.swing.JOptionPane;

公共(public)类 ComboLockerDrive {

public static void main(String[] args) {




    int digit1 = Integer.parseInt(JOptionPane.showInputDialog("Enter the first digit of your locker combo: "));
    int digit2 = Integer.parseInt(JOptionPane.showInputDialog("Enter the second digit of your locker combo: "));
    int digit3 = Integer.parseInt(JOptionPane.showInputDialog("Enter the third digit of your locker combo: "));
    System.out.println("The code has been set!");

     int[] combination = new int[3];
     combination[0] = digit1;
     combination[1] = digit2;
     combination[2] = digit3;

     System.out.println("Now we will try and open the lock!");

     int[] attemptedGuess = new int[3];

     int Guess1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first guess: "));
     int Guess2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second guess: "));
     int Guess3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third guess: "));

     attemptedGuess[0] = Guess1;
     attemptedGuess[1] = Guess2;
     attemptedGuess[2] = Guess3;

     if(combination == attemptedGuess) {
         System.out.println("The lock opened, congratulations, you got the combination right!");
     } 

     else {
         System.out.println("The lock does not open, the combination you tried was incorrect");
     }


}

这是我目前的结果,没有任何错误,但是当我输入正确的组合时,它说组合不正确。这是否意味着我没有正确填写数组?

最佳答案

您不能仅仅为了相等而匹配两个数组,因为当您执行 combination == attemptedGuess 时,您正在尝试匹配始终不同的引用。相反,您应该匹配索引处的各个元素。

您基本上可以匹配所有数字,如果其中一个不匹配,您可以说组合不正确,否则就是正确的。

是这样的: 替换代码

if(combination == attemptedGuess) {
     System.out.println("The lock opened, congratulations, you got the combination right!");
 } 
 else {
     System.out.println("The lock does not open, the combination you tried was incorrect");
 }

有了这个

        for(int i=0;i<3;i++){
            if(attemptedGuess[i]!=combination[i]){
                System.out.println("The lock does not open, the combination you tried was incorrect");
                return;
            }
        }
        System.out.println("The lock opened, congratulations, you got the combination right!");

或者简单地按照@sam的建议

if(Arrays.equals(attemptedGuess, combination)){
    System.out.println("The lock opened, congratulations, you got the combination right!");
}else{
    System.out.println("The lock does not open, the combination you tried was incorrect");
}

关于java - 使用数组存储多个用户生成的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33157687/

相关文章:

java - Matlab 在 jar 包中看不到我的一些 java 类(不是全部)

java - 无法对以下 TreeMap 进行排序

android - ListView 不显示任何项目

java.lang.AbstractMethodError : org. powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable

java - 当常量数量巨大时,如何正确定义常量?

php - PDO:多。行和列 - 如何循环数组?

ruby-on-rails - 如何允许数组具有强参数

javascript - 定义多个数组时,数组或迭代器中的每个子元素都应具有唯一的 "key"属性

java - java中素数计数器从使用方法改为使用嵌套循环

按降序创建两个数组,分别为第一个偶数和第二个奇数