亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓掃描器理解我寫的量?

如何讓掃描器理解我寫的量?

收到一只叮咚 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 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號