为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

收到一只叮咚 2023-03-02 09:48:01
这只是一些作业,但我无法找到一种方法来实现它。请看看我的代码。输入数字后,我只想玩弄它们。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
查看完整描述

2 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

考虑一个for循环:

for(int i = 0; i < amountofNumbers; i++){
    // add to a collection / array / list
    }

然后从那里访问您需要的内容。


查看完整回答
反对 回复 2023-03-02
?
慕村9548890

TA贡献1884条经验 获得超4个赞

import java.util.*; 

class EnterNumber{


    public void enter_list_function(){


        Scanner sc = new Scanner(System.in);


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


        //Enter the Numbers of amount

        int input = sc.nextInt();

        ArrayList<Integer> list = new ArrayList<Integer>();


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

        //Input - Numbers of amount

        for(int j =1; j<=input; j++ ){

            int addval = sc.nextInt();

            //Add the enter amount in array

            list.add(addval);


        }


        Iterator itr=list.iterator();  

        while(itr.hasNext()){  

           //get the enter amount from list

           System.out.println(itr.next());  

        }  

    }



    public static void main(String[] args){


        EnterNumber EnterNumber = new EnterNumber();

        EnterNumber.enter_list_function();


    }


}


查看完整回答
反对 回复 2023-03-02
  • 2 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信