java 读取未知数量的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12720133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:57:41  来源:igfitidea点击:

Reading an unknown number of inputs

javac++input

提问by tianz

I need to read an unknown number of inputs using either C++ or Java. Inputs have exactly two numbers per line. I'd need to use cinor a System.inScannerbecause input comes from the console, not from a file.

我需要使用 C++ 或 Java 读取未知数量的输入。输入每行正好有两个数字。我需要使用cinor aSystem.inScanner因为输入来自控制台,而不是来自文件。

Example input:

示例输入:

1 2

3 4

7 8

100 200

121 10

I want to store the values in a vector. I have no idea how many pairs of numbers I have. How do I design a whileloop to read the numbers so I can put them into a vector?

我想将值存储在向量中。我不知道我有多少对数字。如何设计一个while循环来读取数字,以便将它们放入向量中?

回答by chris

You can use an idiomatic std::copyin C++: (see it work here with virtualized input strings)

您可以std::copy在 C++ 中使用惯用语:(请参阅此处使用虚拟化输入字符串工作

std::vector<int> vec;
std::copy (
    std::istream_iterator<int>(std::cin), 
    std::istream_iterator<int>(), 
    std::back_inserter(vec)
);

That way, it will append onto the vector each time an integer is read from the input stream until it fails reading, whether from bad input or EOF.

这样,每次从输入流中读取整数时,它都会附加到向量上,直到读取失败,无论是从错误输入还是 EOF。

回答by Osiris

Java:

爪哇:

Scanner sc = new Scanner(System.in);
String inputLine;
while(sc.hasNextLine()) {
  inputLine = sc.nextLine();
  //parse inputLine however you want, and add to your vector
}

回答by Rao Virinchi

Instead of using a buffered reader, one can use Scanner as follows to accomplish the same

除了使用缓冲读取器之外,您还可以使用 Scanner 来完成相同的操作

import java.util.*;

public class Solution {

公共课解决方案{

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    while(true)
    {
        String Line = new String(scan.nextLine());
        if(Line.length()==0)
        {
            break;
        }
    }
}

}

}

回答by Hrithik Raj

For someone looking for less fancier C++ code:

对于寻找不那么花哨的 C++ 代码的人:

    #include<iostream>
    #include<vector>
    int main(){
        std::vector<int>inputs;  //any container
        for(int i;std::cin>>i;)  //here the magic happens
            inputs.push_back(i); //press Ctrl+D to break the loop
        for(int num:inputs)      //optional 
            std::cout<<num<<endl;
    }

回答by vaibhav taneja

Only workaround I found :

我发现的唯一解决方法:

import java.io.*;

class Solution {
    public static void main(String args[] ) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int i=1 ;
        String line =br.readLine();
        while(line.length()>0){
            System.out.println(line);
            line = br.readLine();
        }    
    }
}