【问题标题】:Scanner input accepting Strings skipping every other input inside a while loop. [duplicate]扫描器输入接受字符串,在 while 循环内跳过所有其他输入。 [复制]
【发布时间】:2013-10-04 08:22:52
【问题描述】:

好的,所以代码很简单。泛型类ourSet,它接受一些元素,将其放入LinkedList,并对这两个集合执行一些功能。

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

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();  
   }
 }

【问题讨论】:

  • 好吧。他没有机会找到“答案”来看看“那些答案”是否没有帮助。但是,嘿,这只是一个评论,不能被否决。只要给他链接,不要刻薄。这是一个宣传“开发人员学习、分享和建立职业”的地方,而不是一个新手害怕问一个该死的问题的地方,很多人肯定是这样的。对于刚起步、无处可去的人来说,这是一个公平的问题。

标签: java string input do-while


【解决方案1】:

原因是,你打电话给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);
}

【讨论】:

  • 太棒了,非常感谢。我没有意识到我调用了两次,现在我意识到这似乎很愚蠢。 +1 再次感谢
  • @user2844764 如果这个答案是您问题的解决方案,请不要忘记接受它。谢谢!
【解决方案2】:

你要求输入两次

do {

    set1.add(input.nextLine()); // you enter a number

}
while (!"EXIT".equals(input.nextLine())); // what if you enter another number? 
// it'll just loop again, skipping that number

您需要请求输入一次,然后使用它来停止或将其添加到您的集合中。

【讨论】:

    【解决方案3】:

    你的循环正在做你告诉它的事情......

    • 读取一行文字并添加到set1
    • 读取一行文本并检查它是否等于"EXIT"
    • 根据需要重复...

    因此,每隔一个请求就会用于检查退出状态。相反,您应该将输入中的文本分配给一个变量并使用它来进行检查,例如...

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

    ps- 是的,我知道,你可以使用 break ;)

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 2015-12-13
      • 2017-05-22
      • 2016-02-21
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多