如何将不同长度的扫描仪用户输入添加到 ArrayList?

我正在尝试构建一个通过扫描程序构建的整数数组列表。用户必须输入 5 到 10 个整数才能使 ArrayList 有效,但在程序运行时,实际输入数将是未知的。


如何构建程序,使其仅在用户输入 5、6、7、8、9 或 10 个整数时运行?


用户应该在一行上输入所有整数,但是我下面尝试的代码在第5个整数之后将其切断,即使后面还有更多整数


public static ArrayList arrayListBuilder() {

    Scanner input = new Scanner(System.in);

    ArrayList<Integer> integerList = new ArrayList();

    System.out.println("Enter 5-10 ints");

    while (input.hasNextInt()) {

        integerList.add(input.nextInt());

        if (integerList.size() > 4 && integerList.size() < 9) {

            break;

        }

    }

    return integerList;

}

谢谢!


慕后森
浏览 98回答 3
3回答

慕田峪9158850

您的问题在于您的 if 语句。看起来您正在尝试确保用户的输入具有 4 个以上的 int 和少于 9 个 int。但是,在实践中发生的事情是,您一次一个int地读取用户的输入,一旦您的用户输入数组大于4,您就会停止。如果您想将其保持在大致相同的格式,可以尝试一些事情。将下限的检查移动到 while 循环之外,以便程序填充数组列表,然后检查它是否足够大。如果它不够大,请循环回。然后在 while 循环内部保持对上限的检查。我相信这将完成你正在努力做的事情。我附上了可能有帮助的示例代码。&nbsp; &nbsp; public static ArrayList arrayListBuilder() {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> integerList = new ArrayList();&nbsp; &nbsp; &nbsp; &nbsp; while (true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integerList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter 5-10 ints");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Boolean rightSize = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (input.hasNextInt()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integerList.add(input.nextInt());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (integerList.size() > 9) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightSize = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (integerList.size() < 5){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightSize = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rightSize){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return integerList;&nbsp; &nbsp; }

SMILET

如果你想建立列表,只要用户输入5,6,7,8,9或10,而你不知道有效输入的实际大小,这是我想到的:import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class ArrayListBuilder {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; arrayListBuilder();&nbsp; &nbsp; }&nbsp; &nbsp; public static List<Integer> arrayListBuilder() {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> referenceList = Arrays.asList(5, 6, 7, 8, 9, 10);&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> inputList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; getUserInput(referenceList, inputList);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(inputList);&nbsp; &nbsp; &nbsp; &nbsp; return inputList;&nbsp; &nbsp; }&nbsp; &nbsp; private static void getUserInput(List<Integer> referenceList, List<Integer> inputList) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter 5-10 ints");&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int userInput = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (referenceList.contains(userInput)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputList.add(userInput);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getUserInput(referenceList, inputList);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }}

慕姐4208626

你对支票有正确的想法,但不要把它放在正确的位置。如果它在循环内部,则每次给出任何输入时都会运行检查。这意味着当列表收到第 5 个元素时,检查将返回 true 并从循环中断。相反,您应该读取尽可能多的输入(或者具有某种最大值,例如10),然后执行检查。它可能看起来像这样:public static ArrayList arrayListBuilder() {&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; ArrayList<Integer> integerList = new ArrayList();&nbsp; &nbsp; System.out.println("Enter 5-10 ints");&nbsp; &nbsp; while (input.hasNextInt()) {&nbsp; &nbsp; &nbsp; &nbsp; integerList.add(input.nextInt());&nbsp; &nbsp; &nbsp; &nbsp; if (integerList.size() > 10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; if (!(integerList.size() >= 5 && integerList.size() <= 10)) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Received too many integers");&nbsp; &nbsp; }&nbsp; &nbsp; return integerList;}这将,而不是检查循环的每次迭代的大小,检查列表的大小是否超出边界,当扫描仪没有更多的整数给出,或者有超过最大值(如果大小大于10,则可以省略)。检查列表的大小是否超出边界。循环完成后,将执行新检查,如果多于或小于所需数字,则会引发错误。如果您希望它不引发错误,则可以进行一次检查以查看它是否高于或低于所需的范围,但执行如下操作:if (integer.size() > 10) {&nbsp; &nbsp; // Clamp the size of the list to the correct length, being 10} else if (integer.size() < 5) {&nbsp; &nbsp; // Ask for more numbers, or throw an error}这将取代循环外部的其他检查,并允许在处理大小时进行更多自定义。该新检查的作用是检查大小是否大于范围的较大边,检查大小是否小于范围的小边。总的来说,它只是将前一个检查分成两个 if 语句,以允许您能够钳夹或请求更多整数。话虽如此,如果它在范围内,它将通过并返回列表。else if
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python