首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java类( System.in)

Java类( System.in)
EN

Stack Overflow用户
提问于 2020-01-31 15:20:48
回答 3查看 165关注 0票数 0

当用户使用System.in输入文本时,下面的代码没有读取或无限循环。如果我将文本硬编码到Scanner变量中,它就能正常工作,所以我不确定这段代码的System.in部分有什么问题。任何帮助都是非常感谢的。

代码语言:javascript
复制
import java.util.Scanner; // needed to use the Scanner class 

public class HW2 { 

    static Scanner in = new Scanner(System.in); 


    public static void main(String [] args) { 

        System.out.println("Enter your line here"); 


        int the =0; 
        int and =0; 
        int is = 0; 
        int was =0; 
        int noword =0; 

        while (in.hasNext()){ 
            String word = in.next(); 

            if (word.equals("the")){ 
                the++; 
            } 
            else if( word.equals("and")){ 
                and ++; 
            } 
            else if (word.equals("is")){ 
                is++; 
            } 
            else if (word.equals("was")){ 
                was++; 
            } 
            else noword++; 

        } 

        System.out.println("The number of occurrences of the was"+ the); 
        System.out.println("The number of occurrences of and was"+ and); 
        System.out.println("The number of occurrences of is was"+ is); 
        System.out.println("The number of occurrences of was was"+ was); 


    } 
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-31 15:43:22

如前所述,附加到System.in的扫描仪将阻塞,同时寻找更多的输入。解决这个问题的一种方法是从扫描器读取一行,标记它,然后以这种方式循环遍历单词。看起来是这样的:

代码语言:javascript
复制
//...
String line = in.nextLine(); // Scanner will block waiting for user to hit enter
for (String word : line.split(" ")){
    if (word.equals("the")) {
        the++;
    }
//...

您总是可以用一个循环结构(代替,同时,同时)代替另一个循环结构。他们都做同样的事情,只是使用不同的语法,使一个比其他更简单的使用取决于不同的情况。因此,如果要使用while循环,可以执行以下操作:

代码语言:javascript
复制
// ...
String line = in.nextLine();
String[] tokens = line.split(" ");
int i = 0;
while (i < tokens.length){
    String word = tokens[i];
    if (word.equals("the")) {
        the++;
    }
// ...
    i++;
} // end of the while loop

但是,我认为for循环在遍历已知数据集的情况下更简洁。如果您有一个未知的数据集,但是有一个已知的退出条件,则行循环更好。

票数 1
EN

Stack Overflow用户

发布于 2020-01-31 15:33:09

因为System.in在程序运行时总是可用的,除非您关闭它。它永远不会退出while循环。所以你可以添加else if (word.equals("exit")) { break; }。这样,每当您键入'exit‘时,它将关闭while循环,并在while循环之后执行代码。

票数 1
EN

Stack Overflow用户

发布于 2020-01-31 19:09:44

视情况而定,你是否只想读一行文字,然后逐个数单词?

因为您只想要一行,所以可以使用Scanner库获取输入字符串,并将字符串拆分为单个单词,然后应用if-语句。类似于:

代码语言:javascript
复制
   public static void main(String [] args) { 

    System.out.println("Enter your line here"); 

    int the =0; 
    int and =0; 
    int is = 0; 
    int was =0; 
    int noword =0; 

    String input = in.nextLine();

    String words[] = input.split(" ");

    for (String s : words) {

        if (s.equals("the")){ 
            the++; 
        } else if( s.equals("and")){ 
            and++; 
        } else if (s.equals("is")){ 
            is++; 
        } else if (s.equals("was")){ 
            was++; 
        } else {
            noword++;
        }

    }

    System.out.println("The number of occurrences of the was: "+ the); 
    System.out.println("The number of occurrences of and was: "+ and); 
    System.out.println("The number of occurrences of is was: "+ is); 
    System.out.println("The number of occurrences of was was: "+ was); 


} 

这样你就根本不需要way循环了。所以它的处理器和内存效率更高。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60006694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档