简体   繁体   English

如何制作一个在 main 方法中保持总数的函数?

[英]How to make a function that keeps a total inside the main method?

I basically need to make a function that accomplishes a basic sum (like x += y).我基本上需要制作一个完成基本总和的函数(如 x += y)。 My limitations are that the function can't actually take anything as the input, and that it needs to keep track of stuff through a for loop.我的限制是该函数实际上不能将任何内容作为输入,并且它需要通过 for 循环跟踪内容。 I've coded an attempt at it (it is only a small chunk of a larger assignment), but mine seems to reset at every loop through;我已经对它进行了编码(它只是更大任务的一小部分),但我的似乎在每次循环时都会重置; it only ever outputs whatever value() equals, and doesn't keep track of itself inbetween iterations of the loop.它只输出 value() 等于的任何值,并且不会在循环的迭代之间跟踪自身。 I've rewritten some stuff to take out what doesn't need to be in there, as without it there would be quite a bit of chaff.我已经重写了一些东西来去掉里面不需要的东西,因为没有它会有相当多的糠秕。 My function references a value() function, but that isn't important to my problem;我的函数引用了一个 value() 函数,但这对我的问题并不重要; just know that it checks the value of something that changes randomly between iterations, and returns that value as a double.只知道它会检查在迭代之间随机变化的值,并将该值作为双精度值返回。 BenchmarkTimer is simply the class of the overall series of functions. BenchmarkTimer 只是整个系列函数的类。

public class BenchmarkTimer{ 

    public double total;

    public double total(){
        total += value();
        return total;
    }
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        for (int l = 1; l <= 4; l++){
            BenchmarkTimer timer1 = new BenchmarkTimer();
            timer1.value();
            System.out.println(timer1.total());
        }
    }

}

What I'm trying to get happen is that timer1.total should be returning the total of the values added up across all iterations.我想要实现的是 timer1.total 应该返回所有迭代中相加的值的总和。 If the first value obtained was 2, it should output 2. The second time through if given a value of 3, I want it to output 5.如果获得的第一个值是 2,它应该输出 2。如果给定值 3,第二次通过,我希望它输出 5。

Right now, it simply outputs whatever the value gained from that loop is.现在,它只是输出从该循环中获得的任何值。 Also, ideally, I don't want moving the declaration of timer1 outside of the loop to the be the solution, as that will require me re-coding quite a bit of other stuff to accommodate.此外,理想情况下,我不希望将 timer1 的声明移到循环之外作为解决方案,因为这将需要我重新编码很多其他东西以适应。 If its the only way then thats fine, but if there are different solutions to be had I'd be glad to hear them.如果这是唯一的方法,那很好,但如果有不同的解决方案,我会很高兴听到它们。 Thank you for your time!感谢您的时间!

ideally, I don't want moving the declaration of timer1 outside of the loop to the be the solution理想情况下,我不希望将 timer1 的声明移到循环之外作为解决方案

Well, without doing that, then you could make a completely separate variable好吧,如果不这样做,那么您可以创建一个完全独立的变量

    int n = Integer.parseInt(args[0]);
    double d = 0;
    for (int l = 1; l <= 4; l++){
        BenchmarkTimer timer1 = new BenchmarkTimer();
        d += timer1.value();
        System.out.println(timer1.total());
    }
    System.out.println(d);

Otherwise, yes, that is the ideal solution because without that, then you're resetting the timer.否则,是的,这是理想的解决方案,因为没有它,那么您将重置计时器。

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

相关问题 java-如何-运行不在主要功能中的总计 - java - How to - Running total that is not in main function 在 main 方法中编写一个函数 - Java - Writing a function inside the main method - Java 如何在main方法中调用另一个方法? - How to call another method inside of the main method? 如何在方法内部制作方法并在Java中捕获方法上方的函数名称,从而使 - How to make method inside method and catch the name of function above method in java which make the 即使主要功能退出后,该java程序如何保持运行? - How this java program keeps running even after main function exits? 我如何区分循环进入主函数内部和循环进入主函数之外的另一种方法? - How I can differentiate between loops come inside the main function and loops come in another method outside the main function? 如何在Main内部制作第二帧? - How to make the second frame inside the Main? 我如何获取这个 function 并将其拆分为主要方法中的 3 个单独的方法? [等候接听] - How do i take this function and split it into 3 separate methods inside the main method? [on hold] 如何在Kotlin中简化主要的Java方法? - How to make main java method simpler in kotlin? 如何在Java的main方法中创建静态数组? - How to make a static array in main method in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM