如何确定最后一个整数何时被输入?Java

huangapple go评论85阅读模式
英文:

How to determine when last integer has been entered? Java

问题

我现在必须输入一个字母来停止 while 循环并打印出数字列表
有没有更好的方法来确定何时输入了最后一个整数

    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入一系列整数值,以发送到服务器");
    List<Integer> numberList = new ArrayList<Integer>();
    while(scanner.hasNextInt()) {
        numberList.add(scanner.nextInt());
    }
    System.out.println(numberList);
    os.write(String.valueOf(numberList));
    scanner.close();
英文:

I currently have to enter a letter to stop the while loop and print out the number list.
Is there a better way to determine when the last int has been entered?

` Scanner scanner = new Scanner(System.in);
            System.out.println(&quot;Enter a sequence of integer values&quot; +
                    &quot; to be sent to the server&quot;);
            List&lt;Integer&gt; numberList = new ArrayList&lt;Integer&gt;();
            while(scanner.hasNextInt()) {
                numberList.add(scanner.nextInt());
            }
            System.out.println(numberList);
            os.write(String.valueOf(numberList));
            scanner.close();`

答案1

得分: 0

尝试一下

final Scanner scanner = new Scanner(System.in);
System.out.println("输入一系列要发送到服务器的整数值");
final List<Integer> numberList = new ArrayList<Integer>();
int scanned;
do {
    final int scanned = scanner.nextInt();
    numberList.add(scanned);
} while (scanner.hasNextInt() && scanned != yourChar);
os.write(String.valueOf(numberList));
scanner.close();

我希望我正确理解了你的问题。

英文:

Try this

final Scanner scanner = new Scanner(System.in);
System.out.println(&quot;Enter a sequence of integer values to be sent to the server&quot;);
final List&lt;Integer&gt; numberList = new ArrayList&lt;Integer&gt;();
int scanned;
do {
    final int scanned = scanner.nextInt();
    numberList.add(scanned);
} while (scanner.hasNextInt() &amp;&amp; scanned != yourChar);
os.write(String.valueOf(numberList));
scanner.close();`

I hope I understood your problem correctly.

答案2

得分: 0

按照以下步骤执行

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

public class Program {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个整数序列以发送到服务器(输入 x 结束)");
        List<Integer> numberList = new ArrayList<Integer>();
        String[] data = scanner.nextLine().split(" ");
        int i = 0;
        while (i < data.length && data[i].matches("\\d+")) {// 正则表达式 \\d+ 只匹配整数
            numberList.add(Integer.parseInt(data[i]));
            i++;
        }
        System.out.println(numberList);
    }
}

**一个示例运行**

输入一个整数序列以发送到服务器输入 x 结束
10 20 30 x
[10, 20, 30]

另外请不要关闭 `System.in` 上的 `Scanner`,因为这也会关闭 `System.in`。

如果有疑问或问题请随时评论
英文:

Do it as follows:

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

public class Program {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println(&quot;Enter a sequence of integer values to be sent to the server (x to terminate)&quot;);
		List&lt;Integer&gt; numberList = new ArrayList&lt;Integer&gt;();
		String[] data = scanner.nextLine().split(&quot; &quot;);
		int i = 0;
		while (i &lt; data.length &amp;&amp; data[i].matches(&quot;\\d+&quot;)) {// The regex \d+ matches only integers
			numberList.add(Integer.parseInt(data[i]));
			i++;
		}
		System.out.println(numberList);
	}
}

A sample run:

Enter a sequence of integer values to be sent to the server (x to terminate)
10 20 30 X
[10, 20, 30]

Also, do not close Scanner for System.in as it also closes System.in as well.

Feel free to comment in case of a doubt/issue.

huangapple
  • 本文由 发表于 2020年4月5日 20:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定