求java输入遇到回车结束输入的代码
```int coefficient = scanner.nextInt();
int index = scanner.nextInt();
YiYuanNode newNode = new YiYuanNode(coefficient,index);
yiYuanLinkList.addNode(newNode);
```
对于这一段话套一个循环,让他遇到回车时结束输入 不如输入-1结束来得简单,回车在这个方法中会被自动丢弃的 不要用nextInt(),用nextString,然后再转成int,规定一个特定字符串作为结束标志 Scanner scanner = new Scanner(System.in);
for (int num : scanner.nextLine().toCharArray()) {
System.out.println("num = " + (num - '0'));
// do something..
} Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String[] split = s.split(" ");
for (String count:
split) {
int index = Integer.parseInt(count);
YiYuanNode newNode = new YiYuanNode(coefficient,index);
yiYuanLinkList.addNode(newNode);
}
数字中间用空格分割,你看看行不行 Whitte 发表于 2021-7-25 17:34
Scanner scanner = new Scanner(System.in);
for (int num : scanner.nextLine().toCharA ...
这个貌似还是不行 kk7467 发表于 2021-7-26 01:13
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
S ...
谢谢哥们,不过还是不行呀 肯定要有一个结束符的,你可以用 StringUtils.isNotBlank(type) 来判断,但是空格+回车也会跳出循环 ArrayList<Integer> arry = new ArrayList<Integer>();
Scanner scannner = new Scanner(System.in);
String string = scannner.nextLine();
Scanner scannner_next = new Scanner(string);
while(scannner_next.hasNextLine()) {
arry.add(scannner_next.nextInt());
}
System.out.println(arry);
}
试试这个看符合要求不。
页:
[1]