英文:
java.util.LinkedHashMap$LinkedKeyIterator infinite loop in LinkedHashSet
问题
System.out.print("输入链哈希集合的大小:");
Scanner s = new Scanner(System.in); // 创建一个 Scanner 对象
int size = s.nextInt();
HashSet<String> lhashset = new HashSet<>(size);
for (int i = 0; i < size; i++) {
System.out.print("输入姓名:");
String name = s.next();
lhashset.add(name);
}
System.out.println("\n输入你想查找的姓名:");
String find = s.next();
if (lhashset.contains(find)) {
System.out.println("\n是的,链哈希集合包含" + find);
System.out.print("你想要移除这个姓名吗?");
String ch = s.next();
String choice = "yes";
if (ch.equals(choice)) {
lhashset.remove(find);
System.out.println("\n元素已移除。");
} else {
System.out.println("\n再见");
}
} else {
System.out.println("不包含这个姓名。");
}
第一个 if 语句正常工作,但是当我尝试进入 else 语句(打印"不包含")时,根据标题的描述,我陷入了无限循环。对于嵌套的 if 语句也是一样的。
英文:
System.out.print("Enter the size of linked hash set: ");
Scanner s = new Scanner(System.in); // Create a Scanner object
int size = s.nextInt();
HashSet<String> lhashset = new HashSet<>(size);
for(int i=0; i<size; i++)
{
System.out.print("Enter the name: ");
String name = s.next();
lhashset.add(name);
}
System.out.println("\nEnter the name you want to find: ");
String find = s.next();
if(lhashset.contains(find))
{
System.out.println("\nYes the Linked Hash Set contains " +find);
System.out.print("Do you want to remove the name? ");
String ch = s.next();
String choice = "yes";
if(ch.equals(choice))
{
lhashset.remove(find);
System.out.println("\nElement removed.");
}
else
{
System.out.println("\nGoodbye");
}
}
else
{
System.out.println("Does not contain name.");
}
The first if statement works fine, but when I try to go to else statement(print"does not contain"), I get an infinite loop as per the heading. The same happens for the nested if statement.
答案1
得分: 0
ch.equals(choice) 将无法工作。 使用正确的语法更新代码。
public class Soln {
public static void main(String[] args) {
Scanner s = new Scanner(System.in); // 创建一个 Scanner 对象
HashSet<String> lhashset = new HashSet<>();
lhashset.add("TEST");
System.out.println("\n输入您想要查找的名称:");
String find = s.next();
if (lhashset.contains(find)) {
System.out.println("\n是的,Linked Hash Set 包含 " + find);
System.out.print("您想要移除这个名称吗? ");
int ch = s.nextInt();
if (ch == 1) {
lhashset.remove(find);
System.out.println("\n元素已移除。");
} else {
System.out.println("\n再见");
}
} else {
System.out.println("不包含该名称。");
}
}
}
英文:
ch.equals(choice) will not work. Updating the code with correct syntax.
public class Soln {
public static void main(String[] args) {
Scanner s = new Scanner(System.in); // Create a Scanner object
HashSet<String> lhashset = new HashSet<>();
lhashset.add("TEST");
System.out.println("\nEnter the name you want to find: ");
String find = s.next();
if(lhashset.contains(find))
{
System.out.println("\nYes the Linked Hash Set contains " +find);
System.out.print("Do you want to remove the name? ");
int ch = s.nextInt();
if(ch == 1)
{
lhashset.remove(find);
System.out.println("\nElement removed.");
}
else
{
System.out.println("\nGoodbye");
}
}
else
{
System.out.println("Does not contain name.");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论