有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAutil。scanner Java scanner类(System.in)

我有下面的代码,当用户使用系统输入文本时,它不会读取或无限循环。在里面如果我将文本硬编码到Scanner变量中,它工作正常,因此我不确定系统出了什么问题。在本代码的一部分中。感谢您的帮助

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


    } 
}

共 (3) 个答案

  1. # 1 楼答案

    如前所述,扫描仪连接到系统。在查找更多输入时,in将被阻止。一种方法是从扫描器中读取一行,标记它,然后以这种方式循环遍历单词。看起来是这样的:

    //...
    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循环会更好

  2. # 2 楼答案

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

  3. # 3 楼答案

    视情况而定,您想只阅读一行文本,然后逐个计算单词数吗

    因为您只需要一行,所以可以使用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循环。因此,它的处理器和内存效率更高