接受字符串的扫描程序输入会跳过while循环内的所有其他输入。 [英] Scanner input accepting Strings skipping every other input inside a while loop.

查看:65
本文介绍了接受字符串的扫描程序输入会跳过while循环内的所有其他输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Alright, so the code is pretty straight forward. Generic class ourSet, that takes in some elements, puts it in a LinkedList, and does some functions on the two sets.

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

My problem is actually quite unrelated the general concept of the project, its more in the "user input interface" I've created. I want it to take in some Strings and add it to the set, then while receiving the string "EXIT" (all caps), to exit the loop, and do the same for the next set. What is happening is that the do while loop is only sending the 1st, 3rd, 5th,.. for all odd numbers.

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() 两次

The reason is, you're calling input.nextLine() twice:

do {

    set1.add(input.nextLine());

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

一种比做一会儿更容易的方法:

A much easier way than do-while is while:

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

这篇关于接受字符串的扫描程序输入会跳过while循环内的所有其他输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆