【发布时间】:2016-01-14 10:50:12
【问题描述】:
我是 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-numer、arr[1] = user-given-number等方式将其插入。试一试,问我们你是否卡住了:) -
@gonzo 还没有走得太远,因为我被困在我应该输入的前几件事上:P,我会尝试为每个人获取一些代码
-
@MPirious 我知道如何获取用户输入,但是所有这些示例都将输入存储到一个变量中,在上面的示例中,它希望我分别存储每个整数,所以我自然认为所有 3 个数字应该存储到一个数组中,而不仅仅是将每个整数保存到 3 个不同的变量中。
-
@Jcrow,正如 gonzo 所说:首先编辑您的问题并输入代码并准确解释您被击中的位置。