私は、ループを使用せずにJavaでの入力の単一行から複数の整数を読むことができます

Ayman101:

私は私がこれを試みたが、無駄に、リストに整数の束を読み取るためにコードの一部を見つける問題を持っています:

public static void main(String[] args){
    int[] a = in.readInts(); //in cannot be resolved
    StdOut.println(count(a)); //StdOut cannot be resolved
}

あなたは私を助けてくださいことはできますか?

打者:

この例のコードを試してみてください、それはあなたのために働くかどうかを確認します。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int amount = 3; // Amount of integers to be read, change it at will.
        Scanner reader = new Scanner(System.in);
        System.out.println("Please input your numbers: ");

        int num; // integer will be stored in this variable.
        // List that will store all the integers.
        ArrayList<Integer> List = new ArrayList<Integer>();
        for (int i = 0; i < amount; ++i) {
            num = reader.nextInt();
            List.add(num);
        }
        System.out.println(List);
    }
}

入力とコンソールでこのコード1 2 3利回り:

Please input your numbers:
1 2 3 
[1, 2, 3]

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=31490&siteId=1