gpt4 book ai didi

java - 我的 if, else 语句有问题

转载 作者:行者123 更新时间:2023-11-30 07:07:24 25 4
gpt4 key购买 nike

我不明白为什么我的程序会跳到最后的“else”语句。这是我的代码,如下,也有点粗糙,因为我刚刚开始使用它,而且我对 Java 很陌生。如果还有什么需要请尽管询问!

我们应该编写的程序用于获取储物柜组合。我已经对我想要的组合进行了硬编码,当我输入 12-34-56 时,程序就会运行。但是,如果我输入 12-12-34 或任何其他变体,它会自动跳到程序末尾并显示“不匹配”,尽管这不应该是结果。相反,它应该打印出“猜测中的 2 个数字彼此重复。组合中出现 2 个数字。”

如果您还有其他需要,请告诉我!谢谢,祝你有美好的一天! (我还使用 java 和 BlueJ 来完成所有这些工作。)

import java.util.*;
public class Combination
{

public static void main(String[] args) {

int lock1 = 12;
int lock2 = 34;
int lock3 = 56;

Scanner input = new Scanner(System.in);

System.out.print("Enter first two digits: ");
int guess1 = input.nextInt();
System.out.print("Enter second two digits: ");
int guess2 = input.nextInt();
System.out.print("Enter last two digits: ");
int guess3 = input.nextInt();

if (lock1 == guess1 && lock2 == guess2 && lock3 == guess3) {
System.out.print("Exact Match! Locker Unlocked");
}
else if (lock1 == guess1 && lock2 == guess3 && lock3 == guess2
|| lock1 == guess2 && lock2 == guess3 && lock3 == guess1
|| lock1 == guess2 && lock2 == guess1 && lock3 == guess3
|| lock1 == guess3 && lock2 == guess2 && lock3 == guess1
|| lock1 == guess3 && lock2 == guess1 && lock3 == guess2)
{
System.out.println("All numbers match but not in the correct order");
}
else if (lock1 == guess1 && lock2 == guess1 && lock3 == guess1
|| lock1 == guess2 && lock2 == guess2 && lock3 == guess2
|| lock1 == guess3 && lock2 == guess3 && lock3 == guess3) {
System.out.println("Guess contains 3 duplicate numbers.");
System.out.println("One number in the guess appears in the combination.");
}
else if (lock1 == guess1 && lock2 == guess1 && lock3 == guess2
|| lock1 == guess1 && lock2 == guess1 && lock3 == guess3
|| lock1 == guess2 && lock2 == guess2 && lock3 == guess3
|| lock1 == guess2 && lock2 == guess2 && lock3 == guess1
|| lock1 == guess3 && lock2 == guess3 && lock3 == guess2
|| lock1 == guess3 && lock2 == guess3 && lock3 == guess1
|| lock1 == guess2 && lock2 == guess1 && lock3 == guess1
|| lock1 == guess3 && lock2 == guess1 && lock3 == guess1
|| lock1 == guess3 && lock2 == guess2 && lock3 == guess2
|| lock1 == guess1 && lock2 == guess2 && lock3 == guess2
|| lock1 == guess1 && lock2 == guess3 && lock3 == guess3
|| lock1 == guess2 && lock2 == guess3 && lock3 == guess3) {
System.out.println("2 numbers in the guess are duplicates of each other." +
"2 numbers guess appear in the combination.");
}
else{
System.out.println("Sorry not a match");
}


}
}

最佳答案

最后两个 else-if 条件有两个问题:

  1. lockguard 需要交换。我们总是想验证所有三个猜测。目前,一个猜测被检查两次/三次,而一个/两个猜测被忽略。特别是倒数第二个 else-if 仅当 lock1 = lock2 = lock3 时才能输入。发现问题的另一种方法:要知道 Guess 包含 3 个重复的数字。,您必须检查所有三个猜测。目前,仅检查一个猜测(每种情况)。

  2. 最后一个 else-if 条件缺少一些析取语句。有 18 种可能的情况需要输入两次相同的猜测,程序只检查其中的 12 种。

以下是现有案例和失踪案例。每个数字代表三个猜测之一。

missing|present
-------+-------
112
113
121
122
131
133
211
212
221
223
232
233
311
313
322
323
331
332

在我将倒数第二个 else-if-条件更改为之后,程序是正确的*

   guess1 == lock1 && guess2 == lock1 && guess3 == lock1
|| guess1 == lock2 && guess2 == lock2 && guess3 == lock2
|| guess1 == lock3 && guess2 == lock3 && guess3 == lock3

最后一个 else-if 条件为

   guess1 == lock1 && guess2 == lock1 && guess3 == lock2
|| guess1 == lock1 && guess2 == lock1 && guess3 == lock3
|| guess1 == lock1 && guess2 == lock2 && guess3 == lock1
|| guess1 == lock1 && guess2 == lock2 && guess3 == lock2
|| guess1 == lock1 && guess2 == lock3 && guess3 == lock1
|| guess1 == lock1 && guess2 == lock3 && guess3 == lock3
|| guess1 == lock2 && guess2 == lock1 && guess3 == lock1
|| guess1 == lock2 && guess2 == lock1 && guess3 == lock2
|| guess1 == lock2 && guess2 == lock2 && guess3 == lock1
|| guess1 == lock2 && guess2 == lock2 && guess3 == lock3
|| guess1 == lock2 && guess2 == lock3 && guess3 == lock2
|| guess1 == lock2 && guess2 == lock3 && guess3 == lock3
|| guess1 == lock3 && guess2 == lock1 && guess3 == lock1
|| guess1 == lock3 && guess2 == lock1 && guess3 == lock3
|| guess1 == lock3 && guess2 == lock2 && guess3 == lock2
|| guess1 == lock3 && guess2 == lock2 && guess3 == lock3
|| guess1 == lock3 && guess2 == lock3 && guess3 == lock1
|| guess1 == lock3 && guess2 == lock3 && guess3 == lock2

* 我使用脚本检查了所有可能的组合。

提示:如果您使用数组和循环,程序会更简单、更清晰且可扩展。

关于java - 我的 if, else 语句有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39839429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com