扫描并将其放入数组

niwlg2el  于 2021-07-12  发布在  Java
关注(0)|答案(5)|浏览(324)

我要扫描一个句子,数一数有多少个单词。然后把句子排列起来。然后打印出来。
直到system.out.println(“단어개수 : " + 计数);但之后就不行了。
导入java.util.scanner;公共课期中考试{

public static void main(String[] args) {

    System.out.println("Insert sentence: ");

    Scanner scanner = new Scanner(System.in);
    int count =1;

    String sentence = scanner.nextLine();   //문자열 읽기    

    for(int i = 0; i < sentence.length(); i++) {   
        if(sentence.charAt(i)==' ') {   //단어 개수를 띄어쓰기 개수로 계산
            count++;

        }
    }
    System.out.println("단어 개수: " + count);

    String[] wordArray = new String[30];   //배열 선언
    int word = wordArray.length;

    for(int j=0; j<word; j++){
        wordArray[j] = scanner.next();            

       System.out.println("" + wordArray[j]);

    scanner.close();
   }
}

}

qmb5sa22

qmb5sa221#

您可以使用简单的方法获得所有这些:

String[] array = yourString.split(" ");
int amountOfWords = array.length;
smdnsysy

smdnsysy2#

Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();

String[] words = sentence.split("[ ]+");
System.out.println("단어 개수: " + words.length);

for(String word : words){
    System.out.println(word);
}
bxjv4tth

bxjv4tth3#

可以使用按空格拆分的字符串来生成这样的数组 String[] wordArray = sentence.split("\\s+"); 代码:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Insert sentence: ");

    Scanner scanner = new Scanner(System.in);
    int count =1;

    String sentence = scanner.nextLine();   //문자열 읽기    

    for(int i = 0; i < sentence.length(); i++) {   
        if(sentence.charAt(i)==' ') {   //단어 개수를 띄어쓰기 개수로 계산
            count++;

        }
    }
    System.out.println("단어 개수: " + count);

    String[] wordArray = sentence.split("\\s+");   //배열 선언

    for(int j=0; j<wordArray.length; j++){       

       System.out.println("" + wordArray[j]);

   }
}
ndasle7k

ndasle7k4#

import java.util.*;
public class Main {

public static void main(String[] args) {

    System.out.println("Insert sentence: ");

    Scanner scanner = new Scanner(System.in);
    int count =1;

    String sentence = scanner.nextLine();   

    for(int i = 0; i < sentence.length(); i++) {   
        if(sentence.charAt(i)==' ') {   
            count++;
        }
    }
    System.out.println("단어 개수: " + count);

    String[] wordArray = new String[] {sentence};

    for(int j=0; j<count; j++){
       System.out.println("" + wordArray[j]);
       scanner.close();
   }
}

}

nue99wik

nue99wik5#

import java.util.*;

public class Solution {
   public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        String sentence = sc.nextLine();
        // finding the number of word
        for (int i=0; i<sentence.length(); i++) {
            if (sentence.charAt(i) == ' ')
                count += 1;
        }
        System.out.println("The count is : " + count);
        // store the sentence to a string array using split(split by the space)
        String [] sentenceToArray = sentence.split(" ");
        // print all elements inside the array
        for (int i=0; i<sentenceToArray.length; i++)
             System.out.print(sentenceToArray[i]);
   }
}

相关问题