Java 如何为用户使用标志变量来结束其输入

Java 如何为用户使用标志变量来结束其输入,java,arraylist,Java,Arraylist,我有一个整数数组列表,它将由用户填充,我想在用户需要之前接受输入。我该怎么做 我已经试过了,但没有让我满意。其中s是用户的最终令牌 for(int i=0;input.nextInt()!='s';i++) { int a=input.nextInt(); number.add(a); } 使用nextInt()。此外,由于迭代计数未知,因此您应该只使用while循环,而不是使用for循环。我已经修改了你的代码。请参阅下面的代码和代码注释 import java.util.Arra

我有一个整数数组列表,它将由用户填充,我想在用户需要之前接受输入。我该怎么做

我已经试过了,但没有让我满意。其中s是用户的最终令牌

for(int i=0;input.nextInt()!='s';i++)
{   int a=input.nextInt();
    number.add(a);
}
使用
nextInt()。此外,由于迭代计数未知,因此您应该只使用
while
循环,而不是使用
for
循环。我已经修改了你的代码。请参阅下面的代码和代码注释

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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Integer> number = new ArrayList<>();
        String endToken = "s"; // this will be the holder of your end token
        while (true) { // iterate while user doesn't input the end token
            try {
                System.out.println("Enter an integer or enter 's' to exit!"); // ask the user for input
                String userInput = input.nextLine(); // get the user input
                if (userInput.equals(endToken)) { // check if user input is end token
                    break; // exit the loop
                }
                int a = Integer.parseInt(userInput); // convert the input into an int
                number.add(a); // add the input to the list
             // if an error inputs a String but not the end token then an exception will occur
            } catch (Exception e) {
                System.out.println("Invalid Input!"); // tell the user that the input is invalid and ask the input again
            }
        }

        input.close(); // don't forget to close the scanner

        // loop below is just used to print the user inputs
        System.out.println("Your inputs are: ");
        for (int i = 0; i < number.size(); i++) {
            System.out.println(i);
        }
    }

}
import java.util.ArrayList;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
ArrayList编号=新的ArrayList();
String endToken=“s”;//这将是您的结束标记的持有者
while(true){//在用户不输入结束标记时迭代
试一试{
System.out.println(“输入一个整数或输入's'退出!”);//要求用户输入
字符串userInput=input.nextLine();//获取用户输入
if(userInput.equals(endToken)){//检查用户输入是否为结束标记
break;//退出循环
}
int a=Integer.parseInt(userInput);//将输入转换为int
number.add(a);//将输入添加到列表中
//如果错误输入字符串而不是结束标记,则会发生异常
}捕获(例外e){
System.out.println(“无效输入!”);//告诉用户输入无效,然后再次询问输入
}
}
input.close();//别忘了关闭扫描仪
//下面的循环仅用于打印用户输入
System.out.println(“您的输入是:”);
对于(int i=0;i
您可以使用正则表达式来解析传递的输入,解决方案将排除浮点和字符串

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner s = new Scanner(System.in);
    ArrayList<Integer> userNumber = new ArrayList<>();
    Pattern p = Pattern.compile("\\d+\\d");
    Matcher m = null ;

    System.out.println("Enter your number list use chanracter 'NO' or 'no' as the terminator: ");
    String input = s.nextLine();
    String[] data = input.split(",");
    for(String word : data) {
        if(word.equals("NO") || word.equals("no")) {
            break;
        }
        //m = p.matcher(word).matches();
        if(p.matcher(word).matches()) {
            m = p.matcher(word);
            m.find();
            int n = Integer.valueOf((m.group()));
            userNumber.add(n);
        }else {
            System.out.println("Data entered " + word + " is not a number : ");
        }

    }
    System.out.println("Numbers entered are : " + userNumber);

}
输出

Data entered 12.1 is not a number : 
Data entered "asd" is not a number : 
Data entered "NO" is not a number : 
Data entered "NO" is not a number : 
Numbers entered are : [12, 123, 123]

为什么这是
while(true)
?它将创建一个无限循环。它不会创建一个无限循环,因为一旦用户输入
endToken
循环将退出。好的,我错过了that@Mehrose你可以检查我的解决方案。如果它解决了您的问题,请不要忘记将其标记为正确答案和/或向上投票:)输入一个非整数,
inputmaschException
willoccur@Mehrose你能分享你正在尝试的输入吗。。。我可能没有得到正确的问题,这将导致不正确的输出。输入以下内容:
12,12.1123,“asd”,123,“NO”,“NO”
预期结果是
[12123123]
,您的结果是
[12]
nextInt()
与整数而不是字符相比可能更有意义。除非你明确想要这个。
Data entered 12.1 is not a number : 
Data entered "asd" is not a number : 
Data entered "NO" is not a number : 
Data entered "NO" is not a number : 
Numbers entered are : [12, 123, 123]