【问题标题】:Java Scanner Class ( System.in)Java 扫描程序类 (System.in)
【发布时间】:2020-01-31 15:20:48
【问题描述】:

当用户使用 System.in 输入文本时,我的以下代码未读取或无限循环。如果我将文本硬编码到 Scanner 变量中,它可以正常工作,所以我不确定此代码的 System.in 部分有什么问题。任何帮助表示赞赏。

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


    } 
}

【问题讨论】:

  • 另外,能否请您正确格式化您的代码,如果有任何缩进更容易理解或查看错误。
  • 对不起,它应该打印计数,但它只是无限循环而不打印字数。如果我对 Scanner 变量中的文本进行硬编码,它会给我准确的计数,因此 System.in 无法正常工作
  • 用户是否希望输入一个扫描仪会破解并计数的句子?
  • Sree,是的,就是这个想法。

标签: java java.util.scanner


【解决方案1】:

如前所述,附加到 System.in 的 Scanner 在寻找更多输入时会阻塞。解决此问题的一种方法是从扫描仪中读取一行,对其进行标记,然后以这种方式遍历单词。看起来像这样:

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

您始终可以将一种循环结构(for、while、do-while)替换为另一种。它们都做同样的事情,只是使用不同的语法使一个比其他的更易于使用,具体取决于具体情况。所以如果你想使用一个while循环,你可以这样做:

// ...
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 循环在循环已知数据集的情况下更清晰。当数据集未知但退出条件已知时,while 循环会更好。

【讨论】:

  • 感谢 Jordan,如果我将其切换为 for 循环,完整的答案是什么?你是说在这种情况下不可能使用 while 循环,除非我添加了一个转义词?
  • 是否需要使用 while 循环?如果是这样,修改东西就很简单了。所有循环结构(for、while、do-while)都可以通过一些调整来相互替换。
  • 不是必需的,但我已经开始走这条路了,想看看是否有办法修改已经完成的工作并继续。
  • 感谢 Jordan,如果要使用 For 循环,只需将您在上面提供的 sn-p 删除即可?
  • @Steven 要使用 for 循环,只需将现有的 while 替换为我的第一个 sn-p 中的 for 行,然后在其前面添加该行以获得完整的输入行。
【解决方案2】:

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

【讨论】:

  • 唯一的问题是,如果退出出现在用户在阅读所有文本之前输入的文本中。我们可以在按 Enter 键时中断吗?
【解决方案3】:

视情况而定,您是否只想阅读 1 行文本,然后单独计算单词?

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

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


} 

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

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多