Java Arraylist,do while循环并调用方法
我需要创建一个数组列表来保存用户输入的电阻值。然后我需要询问用户使用哪种类型的方法来计算他的答案,然后返回答案。do while循环需要不断询问电阻值,直到用户输入数字0。计算必须从一级阻力中得出 ***类***Java Arraylist,do while循环并调用方法,java,class,methods,arraylist,do-while,Java,Class,Methods,Arraylist,Do While,我需要创建一个数组列表来保存用户输入的电阻值。然后我需要询问用户使用哪种类型的方法来计算他的答案,然后返回答案。do while循环需要不断询问电阻值,直到用户输入数字0。计算必须从一级阻力中得出 ***类*** import java.util.ArrayList; public class Resistance { /** * Holds an object type. */ public int userChoice; ArrayL
import java.util.ArrayList;
public class Resistance {
/**
* Holds an object type.
*/
public int userChoice;
ArrayList<Double> resistor = new ArrayList<Double>();
/**
*Chooses which process follows next.
*@return circuitType
*/
public final double calculateResistance() {
if (userChoice == 1) {
return calculateSeriesResistance();
} else {
return calculaParallelResistance();
}
}
/**Returns object of parallel circuit.
*
* @return 1 / runningResistance.
*/
public double calculaParallelResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.lenght; ++index) {
runningResistance = runningResistance
+ +1 / resistor.lenght;
}
return 1 / runningResistance;
}
/**Returns object of series circuit.
*
* @return runningResistance.
*/
private double calculateSeriesResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.length; ++index) {
runningResistance = runningResistance + resistor[index];
}
return runningResistance;
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
/**
* Makes a Constant.
*/
public final static int DONE = 0;
/**
* Makes a Constant.
*/
public static final int USER_CHOICE_SERIES = 1;
/**
* Makes a Constant.
*/
public static final int USER_CHOICE_PARALLEL = 2;
public static void main(final String[] args) {
// TODO Auto-generated method stub
DecimalFormat f = new DecimalFormat("##.00");
Resistance myRes = new Resistance();
Scanner keyboard = new Scanner(System.in);
//Display Purpose
System.out.println("\nThis program will calculate "
+ "the resistance of a Parallel or Series "
+ "Circuit\n");
//Display instructions
System.out.println("Please enter the value of your "
+ "resistors separately, when done press 0 (zero)");
int n = 0;
do {
System.out.println("Enter Resistor #" + ++n);
myRes.resistor.add(keyboard.nextDouble());
} while (myRes.resistor > 0);
// Ask Which Method To Use,
do {
System.out.println("\nEnter 1 to calculate "
+ "a Series Circuit "
+ "or 2 for a Parallel Circuit");
myRes.userChoice = keyboard.nextInt();
// Ask user again in case he enters something else
} while (myRes.userChoice != USER_CHOICE_PARALLEL
&& myRes.userChoice != USER_CHOICE_SERIES);
//Output the total resistance
System.out.printf("\nThe Total Resistance is "
+ f.format(myRes.calculateResistance()));
}
}
如前所述,您的代码几乎没有问题,让我们一步一步地将其分解 首先,ArrayList没有名为length的字段,您需要使用size()来代替 其次,你计算并联电阻的方法是错误的。您并不是在电阻器上迭代,而是使用电阻器的数量来计算。正确的公式是: 1/Rtotal=1/R1+1/R1+1/R3+ 将代码更改为:
public double calculaParallelResistance() {
double runningResistance = 0;
//use size() instead of length, which doesn't exist in ArrayList
for (int index = 0; index < resistor.size(); index++) {
//also, iterate over each resistor's value, and to get it,
//use...err, get() method :)
runningResistance += (1 / resistor.get(index));
}
return 1 / runningResistance;
}
那么,你有问题吗?那么。。这是我的密码。做你该做的,Stackoverflow?我在尝试创建Do while循环和停止它的sentinel值时遇到了问题。另外,整个过程是为arraylist添加输入,但不起作用。您的代码甚至无法编译。有很多问题,例如ArrayList没有长度字段…非常感谢Melquiades
private double calculateSeriesResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.size(); index++) {
runningResistance += resistor.get(index);
}
return runningResistance;
}
//holds current resistor's value
Double val;
while(true) {
System.out.println("Enter Resistor #" + ++n);
val = keyboard.nextDouble();
//only add if higher than 0
if (val > 0.0) {
myRes.resistor.add(val);
}
//0 entered, so finish the loop
else break;
}