How do I exit a while loop by pressing enter?

前端 未结 3 409
感情败类
感情败类 2021-01-16 13:55

I am trying to get a while loop to break by pressing the Enter key on a keyboard. My code is:

package javaapplication4;
import java.util.ArrayList         


        
3条回答
  •  既然无缘
    2021-01-16 14:38

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class JavaApplication4 {
    
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            ArrayList numbers = new ArrayList();
            System.out.println("Please enter the numbers seperated by a space: ");
    
            String line = keyboard.nextLine();
            StringTokenizer token = new StringTokenizer(line, " ");
            while(token.hasMoreTokens()) {
                numbers.add(Double.parseDouble(token.nextToken()));
            }
    
            System.out.println("Numbers: " + numbers);
    
        }
    

    }

提交回复
热议问题