简体   繁体   English

如何在 Java 中以双精度形式返回数组的元素?

[英]How do I return an element of an array in Java as a double?

I keep receiving an error in the below codetelling me that the type must be an array but its returning a double for "total[agentnumber]" in the print statement.我一直在下面的代码中收到一个错误,告诉我该类型必须是一个数组,但它在打印语句中为“total[agentnumber]”返回一个双精度值。 Can anyone explain this and how I might solve it in this context?任何人都可以解释这一点以及我如何在这种情况下解决它?

My goal: Write code where I input the number of periods, interest rates, and number of agents (where each agent's is assigned a principal equal to its agent number ie agent 1 receive 100, agent 2 receives 200, etc.) and print the balances of each agent.我的目标:编写代码,在其中输入期限数、利率和代理数量(其中每个代理分配的本金等于其代理编号,即代理 1 收到 100,代理 2 收到 200 等)并打印每个代理的余额。 I want to do this in a loop rather than use the compound interest formula because I am trying to learn how to use Java.我想在循环中执行此操作而不是使用复利公式,因为我正在尝试学习如何使用 Java。

Other issue: This is a bit crazily set up because I do not know how to return the results of the for loops.其他问题:这个设置有点疯狂,因为我不知道如何返回 for 循环的结果。 Where should I place "return" and what should I be returning, supposing that I am interested in ultimately printing the equivalent of agent[agentnumber].money for each of the agents.我应该在哪里放置“返回”以及我应该返回什么,假设我有兴趣最终为每个代理打印等价的 agent[agentnumber].money。

public class Person{

    public double money; // initialize

    Person(double cash){
        money = cash;
    }
}
public class Bank {

    public static double bank(double rate, double periods, int agents){
        Person[] agent = new Person[agents];
        double total;

        for( int agentnumber = 0; agentnumber < agents; agentnumber++) {
           agent[agentnumber] = new Person((agentnumber+1)*100); // assign starting incomes and instantiate players

            for(int months = 0; months < periods; months++){
                agent[agentnumber].money = agent[agentnumber].money*(1.0 + rate);
                total = agent[agentnumber].money;
                System.out.println("The total balance of player " + agentnumber + " is " + total[agentnumber]);
            }
        }
    }

    public static void main(String[] args) {
        bank(0.05,120,3);
    }
}

total是一个双total[agentnumber] ,所以只需打印total而不是total[agentnumber]

I keep receiving an error in the below code telling me that the type must be an array but its returning a double for "total[agentnumber]" in the print statement.我一直在下面的代码中收到一个错误,告诉我该类型必须是一个数组,但它在打印语句中为“total[agentnumber]”返回一个双精度值。 Can anyone explain this and how I might solve it in this context?任何人都可以解释这一点以及我如何在这种情况下解决它?

You get this error message because total is a double, not an array.您收到此错误消息是因为total是一个双精度值,而不是一个数组。 In order to use [] the type must be an array.为了使用[] ,类型必须是一个数组。 The solution is to print total instead of total[agentnumber] .解决方案是打印total而不是total[agentnumber]

Where should I place "return" and what should I be returning, supposing that I am interested in ultimately printing the equivalent of agent[agentnumber].money for each of the agents.我应该在哪里放置“返回”以及我应该返回什么,假设我有兴趣最终为每个代理打印等价的 agent[agentnumber].money。

As your program is written right now, you'll get an error because you have declared that bank() will return a double, but you have no return statement in bank() .由于您的程序现在正在编写,您将收到一个错误,因为您已声明bank()将返回一个双精度值,但您在bank()没有return语句。 It's unclear why you'd want to return anything from bank() .目前尚不清楚您为什么要从bank()返回任何内容。 You probably want to change你可能想改变

public static double bank(double rate, double periods, int agents)

to

public static void bank(double rate, double periods, int agents)

and then you don't need to return anything.然后你不需要返回任何东西。

What and where should I be returning?我应该返回什么以及在哪里返回?

You seem to just want to print out the values of an array, so nothing needs returned.您似乎只想打印出数组的值,因此不需要返回任何内容。 In that case, you could make the method have a void return type.在这种情况下,您可以使该方法具有void返回类型。 It also isn't really clear why you needed the array because the local agents variable isn't needed by the main method, so if you did want the array, you should return Person[] instead of double ...也不清楚为什么需要数组,因为main方法不需要本地agents变量,所以如果你确实想要数组,你应该返回Person[]而不是double ...

You could also clean up the code like so.你也可以像这样清理代码。

public class Bank {

    public static Person[] bank(double rate, double periods, int agents){
        Person[] agent = new Person[agents];

        for( int agentnumber = 0; agentnumber < agents; agentnumber++) {
           Person p = new Person((agentnumber+1)*100); // assign starting incomes and instantiate players

            for(int months = 0; months < periods; months++){
                p.money = p.money*(1.0 + rate);
                System.out.printf("The total balance of player %d at month %d is %.2f\n", agentnumber, months, p.money);
            }

            agent[agentnumber] = p;
        }

        return agent;
    }

    public static void main(String[] args) {
        Person[] agents = bank(0.05,120,3);
    }
}

I've had a go at rewriting your code above and ensured it compiles.我已经尝试重写上面的代码并确保它可以编译。 There are a few best practice things I would change if this wasn't just a demonstration which I've added in comments如果这不仅仅是我在评论中添加的演示,我会更改一些最佳实践

public class Bank {

    public static void main(String[] args) {
        Bank bank = new Bank(0.05,120,3);
    }

    // For readability and simplification I would make this public and
    // move it into it's own file called Person.java 
    private class Person {
        public double money;

        Person(double cash){
            money = cash;
        }
    }

    public Bank(double rate, double periods, int numberOfAgents) {
        Person[] agent = new Person[numberOfAgents];

        // Typical java convention is to use camel case so
        // agentnumber would become agentNumber
        for( int agentnumber = 0; agentnumber < numberOfAgents; agentnumber++) {
            agent[agentnumber] = new Person((agentnumber+1)*100);

            for(int months = 0; months < periods; months++){
                agent[agentnumber].money = agent[agentnumber].money*(1.0 + rate);
            }
            System.out.println("The total balance of player " + agentnumber + " is " + agent [agentnumber].money);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM