【问题标题】:Using an array to store multiple user generated integers使用数组存储多个用户生成的整数
【发布时间】:2016-01-14 10:50:12
【问题描述】:

Description of Example

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

package ComboLockerDrive;

导入 javax.swing.JOptionPane;

公共类 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");
     }


}

}

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

【问题讨论】:

  • 如果你创建了一个名为 arr 的大小为 3 的数组,那么你可以通过 arr[0]= user-given-numerarr[1] = user-given-number 等方式将其插入。试一试,问我们你是否卡住了:)
  • @gonzo 还没有走得太远,因为我被困在我应该输入的前几件事上:P,我会尝试为每个人获取一些代码
  • @MPirious 我知道如何获取用户输入,但是所有这些示例都将输入存储到一个变量中,在上面的示例中,它希望我分别存储每个整数,所以我自然认为所有 3 个数字应该存储到一个数组中,而不仅仅是将每个整数保存到 3 个不同的变量中。
  • @Jcrow,正如 gonzo 所说:首先编辑您的问题并输入代码并准确解释您被击中的位置。

标签: java arrays


【解决方案1】:

你不能仅仅为了相等而匹配两个数组,因为当你做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");
}

【讨论】:

  • 你知道我的代码有什么问题吗?我的基本一样,只是我没有for循环,除非它应该在那里?
  • 不能简单的匹配数组对象。它们总是不同的,因为它们的地址会不同。
  • 谢谢!那么究竟是什么阻止了地址的改变呢?是 for 循环有帮助吗?
  • 你也可以使用Arrays.equals(attemptedGuess, combination)
  • what exactly stops the addresses from changing? 是什么意思。当您分配一个数组时,该数组存储在内存的某个地址。所以两个数组的地址会不同,你的陈述总是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多