当前位置: 首页 > 面试题库 >

接受字符串的扫描程序输入会跳过while循环内的所有其他输入。

冯风史
2023-03-14
问题内容

好的,代码很简单。包含一些元素的通用类ourSet将其放入LinkedList中,并对这两个集合执行某些功能。

我的问题实际上与项目的总体概念无关,更多的是在我创建的“用户输入界面”中。我希望它接受一些字符串并将其添加到集合中,然后在接收字符串“
EXIT”(全部大写)的同时退出循环,并对下一个集合进行相同的操作。发生的事情是do while循环仅发送所有奇数的1st,3rd,5th..。

package set.pkgclass;

import java.util.Scanner; 
import java.util.LinkedList;


public class SetClass {


public static void main(String[] args) {

    ourSet<String> set1 = new ourSet<String>();  
    ourSet<String> set2 = new ourSet<String>(); 
    Scanner input = new Scanner(System.in);



    System.out.println("Please enter a string to put in set 1, "
            + "type EXIT (in all caps) to end.");


    do {

        set1.add(input.nextLine());

    }
    while (!"EXIT".equals(input.nextLine()));


    System.out.println("Please enter a string to put in set 2, "
            + "type EXIT (in all caps) to end");

    do {

        set2.add(input.nextLine());
    }
    while (!"EXIT".equals(input.nextLine()));



    ourSet.intersection(set1,set2); 
    ourSet.difference(set1, set2);
    ourSet.union(set1, set2);








     }
  }

class ourSet<T>{




private LinkedList<T> mySet = new LinkedList<>();



public void add(T element){      
    mySet.add(element);
}

public void remove(T element){        
    mySet.remove(element);
}

public boolean membership(T element){        
    if(mySet.contains(element) == true) {
        return true; 
    }

    else {
    return false;
    }
}


public static <T> void union(ourSet<T> s1, ourSet<T> s2){
    System.out.print("The union is: ");
    for (int i=0; i < s1.mySet.size(); i++) {
        T t = s1.mySet.get(i);
        if (!s2.mySet.contains(t)){
            s2.add(t);
            }

    }

    for (int i=0; i < s2.mySet.size(); i++){
        T t = s2.mySet.get(i);
        System.out.print(t+", ");
    }
    System.out.println();


}
public static <T> void intersection(ourSet<T> s1, ourSet<T> s2){ 
    System.out.print("The intersection is: ");
    for (int i=0; i < s1.mySet.size(); i++) {
        T t = s1.mySet.get(i); 
        if (s2.mySet.contains(t)) {
            System.out.print(t+", ");
        }
    }
    System.out.println();

}

public static <T> void difference(ourSet<T> s1, ourSet<T> s2){
    System.out.print("The difference is: ");
    for (int i=0; i < s1.mySet.size(); i++) {
        T t = s1.mySet.get(i);
        if (!s2.mySet.contains(t)) {
            System.out.print(t+", ");
        }

    }
    System.out.println();  
   }
 }

问题答案:

原因是,您打了input.nextLine() 两次 电话:

do {

    set1.add(input.nextLine());

}
while (!"EXIT".equals(input.nextLine()));

比do-while更容易的方法是while:

while (!(String in = input.nextLine()).equals("EXIT")) {
    set1.add(in);
}


 类似资料:
  • 从指定范围(0,20)内且为int的用户处获取有效整数的最佳方法是什么。如果他们输入了无效的整数打印输出错误。 我在想这样的事情: 这是正确的还是有更好的方法?

  • 问题内容: 我正在编写一个使用Event类的程序,该类中有一个日历实例和一个String类型的描述。创建事件的方法使用扫描仪获取月,日,年,小时,分钟和说明。我遇到的问题是Scanner.next()方法仅返回空格之前的第一个单词。因此,如果输入为“我的生日”,则该事件实例的描述就是“我的”。 我做了一些研究,发现人们使用Scanner.nextLine()解决此问题,但是当我尝试这样做时,它只是

  • 我需要创建一个简单的游戏,其中创建了一个随机数,用户必须猜测数字输入到扫描仪。如果他们的猜测太高,系统会告诉他们猜测得更低,如果猜测得太低也是一样。 我正在使用while循环,但我不知道如何连续调用扫描器,以便用户可以不断猜测。以下是我到目前为止的代码:

  • 我需要在while循环之前输入一个用户,这样它就会知道程序是否想继续。问题是while条件的更内部看不到扫描仪之前输入的一行。有什么建议吗?

  • 我开发了一个字符排序器,我想每次字符串排序后提示用户输入一个新的字符串。我遇到的问题是扫描仪一直在扫描用户的第一个输入。如果我使用scanner.next(),它不会计算输入末尾的空白,这不是解决方案。 这是while循环的开始。一旦代码完成,它将再次从“inputtext”开始。