Java 如何在“键盘流”中终止“System.in”;输入“;连续按两次键?
例如,用户可以在我的程序中输入类似的输入:Java 如何在“键盘流”中终止“System.in”;输入“;连续按两次键?,java,io,Java,Io,例如,用户可以在我的程序中输入类似的输入: 71 117 48 115 127 125 117 48 121 126 48 96 117 113 115 117 或者像这样: 71 117 48 115 127 125 117 48 用户只能通过连续按两次“Enter”键来终止输入流 我该怎么做 import java.util.ArrayList; import java.util.Scanner; public class Main { public static
71 117 48 115 127 125 117 48 121 126 48 96 117 113 115 117
或者像这样:
71 117 48
115
127
125 117 48
用户只能通过连续按两次“Enter”键来终止输入流
我该怎么做
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> integers = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
integers.add(scanner.nextInt());
}
}
}
import java.util.ArrayList;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
ArrayList整数=新的ArrayList();
扫描仪=新的扫描仪(System.in);
while(scanner.hasNextInt()){
add(scanner.nextInt());
}
}
}
hasNextInt
和nextInt
忽略空白;所以你不能用它们。您可以使用hasNextLine
和nextLine
(存储前一行);然后解析来自每个输入行的值(在两个空行上停止)。此外,您还可以使用菱形操作符
(我建议对接口进行编程(而不是具体的ArrayList
实现)
public static void main(String[] args) {
List<Integer> integers = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String prevLine = "";
String line = null;
while (scanner.hasNextLine()) {
if (line != null) {
prevLine = line;
}
line = scanner.nextLine().trim();
if (line.isEmpty() && prevLine.isEmpty()) {
break;
}
String[] parts = line.split("\\s+");
for (String p : parts) {
integers.add(Integer.parseInt(p));
}
}
}
publicstaticvoidmain(字符串[]args){
列表整数=新的ArrayList();
扫描仪=新的扫描仪(System.in);
字符串prevLine=“”;
字符串行=null;
while(scanner.hasNextLine()){
如果(行!=null){
prevLine=行;
}
line=scanner.nextLine().trim();
if(line.isEmpty()&&prevLine.isEmpty()){
打破
}
String[]parts=line.split(\\s+);
用于(字符串p:零件){
add(Integer.parseInt(p));
}
}
}
hasNextLine
和nextInt
忽略空白;因此您不能使用它们。您可以使用hasNextLine
和nextLine
(存储前一行);然后解析来自每个输入行的值(停在两个空行上)。此外,您还可以使用菱形运算符
(我建议编程到接口(而不是具体的ArrayList
实现)
public static void main(String[] args) {
List<Integer> integers = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String prevLine = "";
String line = null;
while (scanner.hasNextLine()) {
if (line != null) {
prevLine = line;
}
line = scanner.nextLine().trim();
if (line.isEmpty() && prevLine.isEmpty()) {
break;
}
String[] parts = line.split("\\s+");
for (String p : parts) {
integers.add(Integer.parseInt(p));
}
}
}
publicstaticvoidmain(字符串[]args){
列表整数=新的ArrayList();
扫描仪=新的扫描仪(System.in);
字符串prevLine=“”;
字符串行=null;
while(scanner.hasNextLine()){
如果(行!=null){
prevLine=行;
}
line=scanner.nextLine().trim();
if(line.isEmpty()&&prevLine.isEmpty()){
打破
}
String[]parts=line.split(\\s+);
用于(字符串p:零件){
add(Integer.parseInt(p));
}
}
}
您可能需要将输入方式从hasNextLine()
更改为hasNextLine()
,将nextLine()
更改为nextLine()
您可能需要将输入方式从
hasNextLine()
更改为hasNextLine()
,并将nextLine()
更改为nextLine()
我发现前面的答案不符合要求。一个解决方案在总共两行空行之后退出循环,而不是在连续两行空行之后退出循环
2 4
<<ENTER>>
543
<<ENTER>>
---loop breaks---
24
543
---环路中断---
如果空行是第一行,则另一个解决方案在单个空行之后退出:
<<ENTER>>
---loop breaks---
---循环中断--
下面,我以不同的方式实现对连续空行的跟踪,以便正确处理这两种情况。此外,为了防止输入不匹配异常,我还在将每个标记添加到整数列表之前验证其是否为整数
public static void main(String[] args) {
// Initialise list to store the integers from input.
List<Integer> integers = new ArrayList<>();
// Initialise keyboard stream to get integers from keyboard.
Scanner inputStream = new Scanner(System.in);
// Declare a scanner to parse the lines read in from the inputStream.
Scanner lineReader;
// Initialise boolean to track whether two consecutive ENTER keys
// have been pressed.
boolean previousLineEmpty = false;
// Continue taking input from the user and adding the integer input to
// a list until ENTER is pressed twice.
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
// Determine whether the loop should break or add the integers in
// the line to the list.
if (line.isEmpty()) {
// If the current line and previous line are empty,
// ENTER was pressed twice. So break.
if (previousLineEmpty) {
break;
}
// Otherwise, this line is empty and is an empty previous line for
// the next iteration.
else {
previousLineEmpty = true;
}
} else {
// Initialise scanner to process tokens in line.
lineReader = new Scanner(line);
// Process the tokens in the non-empty line, adding integers to the
// list and ignoring non-integers.
while (lineReader.hasNext()) {
// Add token to list if it is an integer.
if (lineReader.hasNextInt()) {
integers.add(lineReader.nextInt());
}
// If token is not an integer, instead advance to next token.
else {
lineReader.next();
}
}
// In the next iteration, this non-empty line is the previous
// line. Set boolean to false.
previousLineEmpty = false;
}
}
publicstaticvoidmain(字符串[]args){
//初始化列表以存储输入的整数。
列表整数=新的ArrayList();
//初始化键盘流以从键盘获取整数。
扫描仪输入流=新扫描仪(System.in);
//声明一个扫描器来解析从inputStream读入的行。
扫描器;
//初始化布尔值以跟踪两个连续的ENTER键
//已经被催促了。
布尔值previousLineEmpty=false;
//继续从用户获取输入并将整数输入添加到
//按两次ENTER键之前的列表。
while(inputStream.hasNextLine()){
String line=inputStream.nextLine();
//确定循环是否应中断或在中添加整数
//这一行被添加到列表中。
if(line.isEmpty()){
//如果当前行和上一行为空,
//回车键按了两次,所以请停止。
如果(上一行为空){
打破
}
//否则,此行为空,并且是的前一行为空
//下一次迭代。
否则{
previousLineEmpty=true;
}
}否则{
//初始化扫描程序以在线处理令牌。
lineReader=新扫描仪(行);
//处理非空行中的标记,将整数添加到
//列出并忽略非整数。
while(lineReader.hasNext()){
//如果标记是整数,则将其添加到列表中。
if(lineReader.hasNextInt()){
add(lineReader.nextInt());
}
//若标记不是整数,则前进到下一个标记。
否则{
lineReader.next();
}
}
//在下一次迭代中,此非空行是上一次迭代
//行。将布尔值设置为false。
previousLineEmpty=false;
}
}
我发现前面的答案不符合要求。一个解决方案在总共两行空行后退出循环,而不是在