“名称不匹配” 打印,但这里打印了 5 次。

huangapple go评论77阅读模式
英文:

"Name Doesn't Match" print but here it printing 5 times

问题

以下是翻译好的部分:

当我输入一个在数组列表中不存在的内容时,“Name Doesn't Match” 会打印 5 次。我希望只打印一次。

  1. 哪一行代码导致“Name Doesn't Match” 打印了 5 次?
  2. 如何只打印一次“Name Doesn't Match”?
public class Arrays {
    public static void main(String[] args) {

        String[] names = {"Meisam", "Raju", "Sasi", "Aju", "Ram"};
        int[] numbers = {123456, 654321, 345678, 953456, 123445};
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }

        System.out.println("Please Enter a Name:  ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        boolean nameMatched = false; // 添加一个变量来追踪是否已经找到匹配的名称

        for (int i = 0; i < names.length; i++) {
            if (name.equals(names[i])) {
                System.out.println(numbers[i]);
                nameMatched = true; // 标记为找到匹配的名称
                break; // 找到匹配后跳出循环
            }
        }

        if (!nameMatched) {
            System.out.println("Name Doesn't Match!"); // 如果没有找到匹配的名称,打印一次
        }
    }
}

输出结果:

Meisam
Raju
Sasi
Aju
Ram
Please Enter a Name:
raj // 输入一个名为 'raj' 的关键词
Name Doesn't Match!
英文:

When I type something which isn't there in array list, "Name Doesn't Match" is printing for 5 times. I want to make this print for only one time.

  1. Which line of code is making "Name Doesn't Match" print 5 times?
  2. how to print "Name Doesn't Match" only 1 time?
public class Arrays {
    public static void main(String[] args) {

String[] names = {&quot;Meisam&quot;, &quot;Raju&quot;, &quot;Sasi&quot;, &quot;Aju&quot;, &quot;Ram&quot;};
        int[] numbers = {123456, 654321, 345678, 953456, 123445};
        for (int i = 0; i &lt; names.length; i++) {
            System.out.println(names[i]);
        }

        System.out.println(&quot;Please Enter a Name:  &quot;);
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();

        for(int i = 0; i &lt; names.length; i++){
            if(name.equals(names[i])) {
                System.out.println(numbers[i]);
            } else {
                System.out.println(&quot;Names Doesn&#39;t Match!&quot;);
            }
            }
        }
    }

Output is:

Meisam
Raju
Sasi
Aju
Ram
Please Enter a Name:  
raj //input a keyword called &#39;raj&#39; 
Names Doesn&#39;t Match!
Names Doesn&#39;t Match!
Names Doesn&#39;t Match!
Names Doesn&#39;t Match!
Names Doesn&#39;t Match!

答案1

得分: 1

// Which line of code is making "Name Doesn't Match" print 5 times?
// 以下是导致 "Name Doesn't Match" 打印 5 次的代码行:

for(int i = 0; i < names.length; i++) {
   // ...
}

// It will print `Names Doesn't Match!` for each element from the array.
// 如果数组中包含10个元素,则会为每个元素打印 `Names Doesn't Match!`。

// how to print "Name Doesn't Match" only 1 time?
// 如何仅打印一次 "Name Doesn't Match" ?

Just check the whole array first and set `a marker` where the name found or not.
首先检查整个数组设置一个标记”,标记名称是否被找到

Only after that if `maker is false`, print the message.
只有在 `marker` 为假时才打印该消息

int j = -1;    // j == -1 -> means NOT_FOUND

for (int i = 0; i < name.length(); i++) {
    if (name.equals(names[i])) {
        j = i;
        break;
    }
}

System.out.println(j == -1 ? "Names Doesn't Match!" : numbers[j]);

---

I think it's better to use `Map` for this issue:
我认为更好的方法是使用 `Map` 来解决这个问题

public static void main(String... args) {
    Map<String, Integer> nameNumber = Map.of(
            "Meisam", 123456,
            "Raju", 654321,
            "Sasi", 345678,
            "Aju", 953456,
            "Ram", 123445);

    nameNumber.keySet().forEach(System.out::println);

    System.out.print("Please Enter a Name:  ");
    String name = new Scanner(System.in).next();
    Integer number = nameNumber.get(name);
    System.out.println(number == null ? "Names Doesn't Match!" : number);
}
英文:

> Which line of code is making "Name Doesn't Match" print 5 times?

for(int i = 0; i &lt; names.length; i++) {
// ...
}

It will print Names Doesn&#39;t Match! for each element from the array. If the array contains e.g. 10 elements, then you'll see 10 time this message.

> how to print "Name Doesn't Match" only 1 time?

Just check the whole array first and set a marker where the name found or not. Only after that if maker is false, print the message.

int j = -1;    // j == -1 -&gt; means NOT_FOUND
for (int i = 0; i &lt; name.length(); i++) {
if (name.equals(names[i])) {
j = i;
break;
}
}
System.out.println(j == -1 ? &quot;Names Doesn&#39;t Match!&quot; : numbers[j]);

I think it's better to use Map for this issue:

public static void main(String... args) {
Map&lt;String, Integer&gt; nameNumber = Map.of(
&quot;Meisam&quot;, 123456,
&quot;Raju&quot;, 654321,
&quot;Sasi&quot;, 345678,
&quot;Aju&quot;, 953456,
&quot;Ram&quot;, 123445);
nameNumber.keySet().forEach(System.out::println);
System.out.print(&quot;Please Enter a Name:  &quot;);
String name = new Scanner(System.in).next();
Integer number = nameNumber.get(name);
System.out.println(number == null ? &quot;Names Doesn&#39;t Match!&quot; : number);
}

答案2

得分: 0

将您的代码更改为设置布尔值,并在循环后进行评估。

此外,您可以在找到循环后立即跳出循环。

boolean found = false;
for (int i = 0; i < names.length; i++) {
    if (name.equals(names[i])) {
        System.out.println(numbers[i]);
        found = true;
        break;
    }
}

if (!found) {
    System.out.println("Names Doesn't Match!");
}
英文:

Change your code to set a boolean value, and evaluate it after the loop.

Also, you can break out of the loop as soon as it is found.

boolean found = false;
for(int i = 0; i &lt; names.length; i++){
if(name.equals(names[i])) {
System.out.println(numbers[i]);
found = true;
break;
}
}
if (!found){
System.out.println(&quot;Names Doesn&#39;t Match!&quot;);
}

答案3

得分: -1

在 for 循环中删除 else 部分
在 for 循环的 if 块内的最后一行添加 System.exit(0);
在 for 循环之后添加 System.out.println("Names Doesn't Match!");

英文:

Remove else part in for loop
Add System.exit(0); at last line of inside of if-block in the for loop
Add System.out.println("Names Doesn't Match!") ; after the for loop

答案4

得分: -2

for (int i = 0; i < names.length; i++) {
    if (name.equals(names[i])) {
        System.out.println(numbers[i]);
    } else {
        System.out.println("Names Don't Match!");
    }
}

该代码会使你的代码打印出字符串5次。这是因为你正在遍历names数组的所有5个元素。

如果你希望它只打印一次,我建议你可以将其放入一个函数中,在匹配结果时返回"Names Don't Match!"

英文:
for(int i = 0; i &lt; names.length; i++) {
    if(name.equals(names[i])) {
        System.out.println(numbers[i]);
    } else {
        System.out.println(&quot;Names Doesn&#39;t Match!&quot;);
    }
}

Is making your code print the string 5 times. It does it, because you are iterating over all 5 elements of your names array.

I would suggest, if you want it to print once, you can put it in a function and return upon a matched result on return &quot;Names Doesn&#39;t Match!

huangapple
  • 本文由 发表于 2020年9月30日 14:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64132209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定