Java:填充2D数组以查找电路的功率

时间:2018-10-18 14:09:36

标签: java arrays 2d physics circuit

嗨,我正在尝试编写一个代码,以使您的电阻恒定且电流不断增加,然后计算功率。我想将所有数据放入一个数组中,以使其变得整洁而简单。但是我在努力填充它,我对使用的语法一无所知,但是我想已经初始化了数组。老实说,任何事情都会有所帮助,谢谢!

package assignment_10_18_2018;

public class lab_10_18_2018_a {

    public lab_10_18_2018_a() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub 
        final int LENGTH_FOR_CURRENT = 11 ; 
        int resistance = 10 ; 
        int[][] circuitArray = new int [10][3]; 

        for(int i = 0; i < 10 ; i++) { 
            for(int r = 0; r < ...; r++) {
                circuitArray[i][r] = ...;  
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您将必须使用简单的if循环将不同的内容放在不同的列中,例如:

//Pseudo code
public class lab_10_18_2018_a {

public lab_10_18_2018_a() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    // TODO Auto-generated method stub 
    final int LENGTH_FOR_CURRENT = 11 ; 
    int resistance = 10 ; 
    int[][] circuitArray = new int [10][3]; 

    for(int i = 0; i < 10 ; i++) { 
        for(int r = 0; r < 3; r++) {
            if(r==0){ //first column
                circuitArray[i][r] = i+1; //current will go from 1 to 10 in this case in the first column. Modify appropriately to suit your needs
            }  
            else if(r==1){ //second column
                circuitArray[i][r] = resistance;
            } 
            else if(r==2){//third column
                circuitArray[i][r] = circuitArray[i][r-2]*circuitArray[i][r-2]*circuitArray[i][r-1]; //Electric Power formula
            } 
        }
    }
}

}

此外,如果您遵循Java命名约定,那就更好了。您可以在https://www.oracle.com/technetwork/java/codeconventions-135099.html

上阅读更多内容

答案 1 :(得分:0)

这里resistance是常量(例如10),所以我们只需要1行,但是根据问题,我们需要将current从0更改为10。所以我们需要一个具有1行的数组和11列。

int power[][] = new int[1][11]; 

            //current increases from 0-10 

            int resistance = 10,i,j;

            for(i=0;i<power.length;i++)
            {
                for(j=0;j<power[i].length;j++)
                {
                    power[i][j] = j*j*resistance;

                }

            }

计算每个当前值的功率值,并将它们存储在power数组中。

答案 2 :(得分:0)

public static void main(String[] args) {
    int resistance = 10 ; 
    //you need 11 rows and 3 columns
    int[][] circuitArray = new int [11][3]; 
    //for each row i set first cell to i 
    //second cell to your constant
    //third cell with callculated value (first_cell * firs_cell * second_cell)=(I*I*R)
    for(int i = 0; i < 11 ; i++) {            
        circuitArray[i][0] = i; 
        circuitArray[i][1] = resistance;
        circuitArray[i][2] = circuitArray[i][0] * circuitArray[i][0] * circuitArray[i][1];
    }
    //print header
    System.out.printf("%-10s %-10s %-10s%n","I(amps)","R(ohms)","P(watts)");
    //print values
    for(int[] row : circuitArray){
       System.out.printf("%-10d %-10d %-10d%n",row[0],row[1],row[2]); 
    }
}