【问题标题】:Arraylist, do while loop and calling a methodArraylist,做while循环并调用方法
【发布时间】:2013-12-22 00:59:51
【问题描述】:

我需要创建一个数组列表来保存用户输入的电阻值。然后我需要询问用户使用哪种方法来计算他的答案,然后返回答案。 do while 循环需要不断询问电阻值,直到用户输入数字零。计算必须从一个类 Resistance 拉到 Main。

*****

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()));
    }    
}

【问题讨论】:

  • 那么,您有什么问题吗?
  • 所以.. 这是我的代码。做你的事吗,Stackoverflow?
  • 我在尝试创建 do while 循环和停止它的哨兵值时遇到问题。整个添加数组列表的输入,也不起作用
  • 您的代码甚至无法编译。有很多问题,例如。 ArrayList 没有长度字段...

标签: java class methods arraylist do-while


【解决方案1】:

如前所述,您的代码几乎没有问题,让我们逐步分解。

首先,ArrayList 没有长度字段,你想使用 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;
}

您的方法 calculateSeriesResistance() 也有一个问题:您无法使用数组表示法访问对象,如电阻 [index]。使用 get() 方法,所以正确的代码是:

private double calculateSeriesResistance() {
    double runningResistance = 0;
    for (int index = 0; index < resistor.size(); index++) {
        runningResistance += resistor.get(index);
    }
    return runningResistance;
}

现在,进入主要方法。您的第一个“执行”循环是检查 ArrayList 中的电阻器数量,而不是用户输入的 0 值。您可以改用 while 循环并在输入 0 时中断:

//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;
}

【讨论】:

  • 非常感谢 Melquiades
猜你喜欢
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多