第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我如何初始化另一個(gè)類的數(shù)組?

我如何初始化另一個(gè)類的數(shù)組?

www說 2022-05-21 17:15:12
我正在嘗試根據(jù)用戶輸入初始化一個(gè)數(shù)組,以便它可以是任何自定義大小。默認(rèn)情況下,我已將所有 0.0 值的數(shù)組初始化為 10,但是當(dāng)我嘗試調(diào)用初始化數(shù)組的方法時(shí),什么也沒有發(fā)生,并且數(shù)組中的大小或值都不會(huì)改變。班級(jí)編號(hào):public class Numbers {private Float [] numbers;private int default_size = 10;public Numbers() {    ///  write code here to intialize a "default" array since this is the default constructor    numbers = new Float[default_size];    for(int i = 0; i < default_size; i++)    numbers [i] = (float) 0.0;}public Numbers (int size) {    ///  write code here to initialize an "initial" array given the parameter size as this is an initial constructor    numbers = new Float[size];}public void initValuesInArray() {    /// write code here to intialize the values in the array    Scanner scan = new Scanner(System.in);    for (int i = 0; i < numbers.length; i++) {        System.out.println("Enter Value : ");        numbers[i] = scan.nextFloat();    }}public String toString() {    // write code to return the values in the array in a list format - one value per line    for (int i = 0; i < numbers.length; i++)    System.out.println(numbers[i]);    return " ";}public float calcAverage() {    // write code to return the average of the values in the array    double sum = 0.0;    for (int i = 0; i < numbers.length; i++)        sum += numbers[i];    double average = sum / numbers.length;    System.out.println(average);    return (float) average;}}主類:public class Main {public static void main (String [] args) {    // write the code here to implement the menu as specified in Lab 1    boolean menuLoop = true;    while(true) {    System.out.println("Enter 1 to initialize a default array");        }    } }}它需要能夠根據(jù)用戶輸入更改數(shù)組的大小,然后用戶可以將值輸入到數(shù)組中。
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

您沒有使用public Numbers (int size)在類中創(chuàng)建的構(gòu)造函數(shù)Numbers。此外,您正在創(chuàng)建一個(gè)名為 的新變量numbers,而不是分配本地字段obj。這:

    Numbers [] numbers = new Numbers[x];

應(yīng)該

    obj = new Numbers(x);


查看完整回答
反對(duì) 回復(fù) 2022-05-21
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

如果您的元素?cái)?shù)量不固定,則不應(yīng)使用 Array。而是使用List或ArrayList(List 的子類)。


您可以在以下位置找到您需要的所有方法: developer.android.com/reference/java/util/ArrayList


編輯 24/01/19:


如果您需要ListorArrayList默認(rèn)為 10,以防用戶不輸入值,您可以創(chuàng)建一個(gè)IF/ELSE自動(dòng)填充列表的,或者使用SWITCH CASE您已經(jīng)擁有的


例如:


List<Integer> myList = new ArrayList<Integer>();

float defaultValue = 0.0;


case 1:

    for(int i = 0; i < 10; i++) {

        myList.add(defaultValue)

    }

    break;

case 2:

    System.out.println("Enter the new size of array: ");

    int x = scan.nextInt();

    for(int i = 0; i < x; i++) {

        myList.add(defaultValue)

    }

    break;


查看完整回答
反對(duì) 回復(fù) 2022-05-21
  • 2 回答
  • 0 關(guān)注
  • 122 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)