[英]Java: filling a 2D array to find Power of a circuit
Hi I am trying to write a code for an assignment where you have a constant resistance and increasing current, then calculate power.嗨,我正在尝试为具有恒定电阻和增加电流的作业编写代码,然后计算功率。 I wanted to put all the data into an array just to make it neat and simple.我想将所有数据放入一个数组中,只是为了使其简洁明了。 But I am struggling to fill it, I have no idea on the syntax to use, but I have initialized the array, I think.但是我正在努力填充它,我不知道要使用的语法,但是我想我已经初始化了数组。 Honestly anything would help, thanks!老实说,任何事情都会有所帮助,谢谢!
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] = ...;
}
}
}
}
You will have to use simple if loops to put different things in different columns, like so:您必须使用简单的 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
}
}
}
}
} }
Also, it would be better if you follow the Java naming conventions.此外,如果您遵循 Java 命名约定会更好。 You could read up more on https://www.oracle.com/technetwork/java/codeconventions-135099.html您可以在https://www.oracle.com/technetwork/java/codeconventions-135099.html上阅读更多内容
Here we need only 1 row as the resistance
is constant(say 10) but as per the question we need to change the current
from 0 to 10. So we need an array having 1 row and 11 column.这里我们只需要 1 行,因为resistance
是恒定的(比如 10),但根据问题,我们需要将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;
}
}
Calculate the value of power for each current value and store them in the power
array.计算每个电流值的 power 值并将它们存储在power
数组中。
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]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.