英文:
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("Enter a sequence of integer values" +
" to be sent to the server");
List<Integer> numberList = new ArrayList<Integer>();
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("Enter a sequence of integer values to be sent to the server");
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();`
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("Enter a sequence of integer values to be sent to the server (x to terminate)");
List<Integer> numberList = new ArrayList<Integer>();
String[] data = scanner.nextLine().split(" ");
int i = 0;
while (i < data.length && data[i].matches("\\d+")) {// 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论