英文:
"Name Doesn't Match" print but here it printing 5 times
问题
以下是翻译好的部分:
当我输入一个在数组列表中不存在的内容时,“Name Doesn't Match” 会打印 5 次。我希望只打印一次。
- 哪一行代码导致“Name Doesn't Match” 打印了 5 次?
- 如何只打印一次“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.
- Which line of code is making "Name Doesn't Match" print 5 times?
- how to print "Name Doesn't Match" only 1 time?
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();
for(int i = 0; i < names.length; i++){
if(name.equals(names[i])) {
System.out.println(numbers[i]);
} else {
System.out.println("Names Doesn't Match!");
}
}
}
}
Output is:
Meisam
Raju
Sasi
Aju
Ram
Please Enter a Name:
raj //input a keyword called 'raj'
Names Doesn't Match!
Names Doesn't Match!
Names Doesn't Match!
Names Doesn't Match!
Names Doesn'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 < names.length; i++) {
// ...
}
It will print Names Doesn'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 -> 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:
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);
}
答案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 < 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!");
}
答案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 < names.length; i++) {
if(name.equals(names[i])) {
System.out.println(numbers[i]);
} else {
System.out.println("Names Doesn't Match!");
}
}
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 "Names Doesn't Match!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论