如何让扫描器理解我写的量?

这只是一些作业,但我无法找到一种方法来实现它。请看看我的代码。输入数字后,我只想玩弄它们。


public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    //user says if he'd like to use 3 numbers for instance (2,4,5)

    System.out.println("How many numbers would you like to use? : ");

    int amountOfNumbers = sc.nextInt();

    System.out.println("Great! Please type in your numbers: ");

    int numbers =sc.nextInt(amountOfNumbers);

    // should let him write the amount of numbers he entered    

}

一旦他输入了他想要使用的数字数量,我希望扫描仪能够让他输入所有这些数字。

假设他想使用的号码数量是三个。我希望他能够像这样在控制台中输入:

  • 第一个数字 + 回车键

  • 第二个数字 + 输入键

  • 第三个数字+回车键

  • 写不下去了

这就是我在这里通过在扫描仪本身中添加“amountOfNumbers”的意思......(这是行不通的)

int numbers =sc.nextInt(amountOfNumbers);

BR


收到一只叮咚
浏览 141回答 2
2回答

波斯汪

考虑一个for循环:for(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;amountofNumbers;&nbsp;i++){ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;add&nbsp;to&nbsp;a&nbsp;collection&nbsp;/&nbsp;array&nbsp;/&nbsp;list &nbsp;&nbsp;&nbsp;&nbsp;}然后从那里访问您需要的内容。

慕村9548890

import java.util.*;&nbsp;class EnterNumber{&nbsp; &nbsp; public void enter_list_function(){&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many numbers would you like to use?");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //Enter the Numbers of amount&nbsp; &nbsp; &nbsp; &nbsp; int input = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> list = new ArrayList<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Great! Please type in your numbers: ");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //Input - Numbers of amount&nbsp; &nbsp; &nbsp; &nbsp; for(int j =1; j<=input; j++ ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int addval = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add the enter amount in array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(addval);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Iterator itr=list.iterator();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while(itr.hasNext()){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//get the enter amount from list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(itr.next());&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; EnterNumber EnterNumber = new EnterNumber();&nbsp; &nbsp; &nbsp; &nbsp; EnterNumber.enter_list_function();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java