用Java对for循环的结果进行求和。

huangapple go评论95阅读模式
英文:

SUM the result of a for LOOP java

问题

所以我创建了一个带有静态元素的for循环一切都正常工作但后来我想对此循环中的值进行求和以获得总值但每次我尝试打印它时得到的值都是0有任何想法吗请原谅这种原始的编码

我还有一个**Console类**我没有贴出来但它来自**Scanner**用于userInput”。

主类
```java
public class Main {

    private static Console Consola;

    public static void main(String[] args) {

        ItemPower oco = new ItemPower();
        CalculateThings oco2 = new CalculateThings();


        System.out.println(Math.ceil(oco.preçoUnitarioVIII_II));

        double unitariaT5 = Consola.userInput("Qual o Preço unitario da T5?");
        double unitariaT4 = Consola.userInput("Qual o Preço unitario da T4?");
        double unitariaCedarLog = Consola.userInput("Qual o Preço unitario da chedar Log?");
        double desejadaT5 = Consola.userInput("Qual o Preço desejada de T5?");





        System.out.println("Investimento em tier 5 Plank é de: " + oco2.tier5Plank(unitariaT5, desejadaT5));
        System.out.println("Investimento em tier 4 Plank é de: " + oco2.tier4Plank(desejadaT5, unitariaT4));
        System.out.println("Investimento em Chedar logs é de: " + oco2.chedarLog(desejadaT5, unitariaCedarLog));

        System.out.println("totaT5的值为:" + oco2.totalT5);

        CalculateThings.loopT5();
            }
    
}

类 Calculate Things:

public class CalculateThings {

     double unitariaT5;
     double unitariaT4;
     double unitariaCedarLog;
     double desejadaT5;
     double totalT5 = 0;
     double totalT4 = 0;
     double totalCH = 0;



    public double tier5Plank(double desejadaT5, double unitariaT5){
     return desejadaT5 *  unitariaT5;

    }

    public double tier4Plank(double desejadaT5, double unitariaT4){

        return desejadaT5 *  unitariaT4;
    }

    public double chedarLog(double desejadaT5, double unitariaCedarLog){


        return desejadaT5 *  unitariaCedarLog * 3;
    }


    public double loopT5(double desejadaT5) {

        for (int i = (int) desejadaT5; i >= 1; i = (int) (i * 0.367)) {
            totalT5 += i;
                      
        }
        return totalT5;
    }

}

感谢您再次提供帮助。


<details>
<summary>英文:</summary>
So, I made a for loop with static and was working fine. But then I wanted to SUM the values of this loop to get the total value. But everytime I try to print it, I get the value 0. Any ideas? Please excuse the primitive coding.
I also have a **Console class** that I don&#39;t post but its from the **Scanner** class and is used for the &quot;userInput&quot;.
Main class:
```java
public class Main {
private static Console Consola;
public static void main(String[] args) {
ItemPower oco = new ItemPower();
CalculateThings oco2 = new CalculateThings();
System.out.println(Math.ceil(oco.pre&#231;oUnitarioVIII_II));
double unitariaT5 = Consola.userInput(&quot;Qual o Pre&#231;o unitario da T5?&quot;);
double unitariaT4 = Consola.userInput(&quot;Qual o Pre&#231;o unitario da T4?&quot;);
double unitariaCedarLog = Consola.userInput(&quot;Qual o Pre&#231;o unitario da chedar Log?&quot;);
double desejadaT5 = Consola.userInput(&quot;Qual o Pre&#231;o desejada de T5?&quot;);
System.out.println(&quot;Investimento em tier 5 Plank &#233; de: &quot; + oco2.tier5Plank(unitariaT5, desejadaT5));
System.out.println(&quot;Investimento em tier 4 Plank &#233; de: &quot; + oco2.tier4Plank(desejadaT5, unitariaT4));
System.out.println(&quot;Investimento em Chedar logs &#233; de: &quot; + oco2.chedarLog(desejadaT5, unitariaCedarLog));
System.out.println(&quot;value of totaT5 is &quot; +oco2.totalT5);
CalculateThings.loopT5();
}
}

Class Calculate Things:

public class CalculateThings {

     double unitariaT5;
     double unitariaT4;
     double unitariaCedarLog;
     double desejadaT5;
     double totalT5 = 0;
     double totalT4 = 0;
     double totalCH = 0;



    public double tier5Plank(double desejadaT5, double unitariaT5){
     return desejadaT5 *  unitariaT5;

    }

    public double tier4Plank(double desejadaT5, double unitariaT4){

        return desejadaT5 *  unitariaT4;
    }

    public double chedarLog(double desejadaT5, double unitariaCedarLog){


        return desejadaT5 *  unitariaCedarLog * 3;
    }


    public double loopT5(double desejadaT5) {

        for (int i = (int) desejadaT5; i &gt;= 1; i = (int) (i * 0.367)) {
            totalT5 += i;
                      
        }
        return totalT5;
    }

}

Thanks again for the help.

答案1

得分: 3

这里附上的代码中存在主要的两个问题。

首先,您没有使用参数调用loopT5()方法。另外,如果您已经有了CalculateThings的实例oco2,您可以这样调用该方法:

oco2.loopT5(desejadaT5);

或者您也可以这样做:

double d = new CalculateThings().loopT5(desejadaT5);
System.out.println("totaT5的值是:" + d);

其次,在调用loopT5()方法之后应该放置打印语句。

英文:

There are mainly 2 issues that I see here in the code attached.

First, you are not calling the loopT5() method with parameter. Also if you have already have instnace oco2 of CalculateThings, you can call the method like this

oco2.loopT5(desejadaT5);

or else you can do it like this

double d = new CalculateThings().loopT5(desejadaT5);
System.out.println(&quot;value of totaT5 is &quot; +d);

Second, print statement should be after calling the loopT5().

答案2

得分: 0

你只需要将这两行替换为以下内容。

oco2.loopT5();
System.out.println("totaT5的值为:" + oco2.totalT5);

对于以下代码,我能够获得所需的输出。

class Main {

    public static void main(String[] args) {

        CalculateThings oco2 = new CalculateThings();

        double unitariaT5 = 5;
        double unitariaT4 = 4;
        double unitariaCedarLog = 10;
        double desejadaT5 = 15;

        System.out.println("Investimento em tier 5 Plank 是:" + oco2.tier5Plank(unitariaT5, desejadaT5));
        System.out.println("Investimento em tier 4 Plank 是:" + oco2.tier4Plank(desejadaT5, unitariaT4));
        System.out.println("Investimento em Chedar logs 是:" + oco2.chedarLog(desejadaT5, unitariaCedarLog));
        oco2.loopT5(5);
        System.out.println("totaT5的值为:" + oco2.totalT5);

    }

}

输出:

Investimento em tier 5 Plank 是:75.0
Investimento em tier 4 Plank 是:60.0
Investimento em Chedar logs 是:450.0
totaT5的值为:6.0
英文:

You just need to replace these two lines as below.

System.out.println(&quot;value of totaT5 is &quot; +oco2.totalT5);
CalculateThings.loopT5();

With below two lines.

oco2.loopT5();
System.out.println(&quot;value of totaT5 is &quot; +oco2.totalT5);

For below code, i am able to get require output

class Main {
public static void main(String[] args) {
CalculateThings oco2 = new CalculateThings();
double unitariaT5 = 5;
double unitariaT4 = 4;
double unitariaCedarLog = 10;
double desejadaT5 = 15;
System.out.println(&quot;Investimento em tier 5 Plank &#233; de: &quot; + oco2.tier5Plank(unitariaT5, desejadaT5));
System.out.println(&quot;Investimento em tier 4 Plank &#233; de: &quot; + oco2.tier4Plank(desejadaT5, unitariaT4));
System.out.println(&quot;Investimento em Chedar logs &#233; de: &quot; + oco2.chedarLog(desejadaT5, unitariaCedarLog));
oco2.loopT5(5);
System.out.println(&quot;value of totaT5 is &quot; + oco2.totalT5);
}
}

Output:
Investimento em tier 5 Plank &#233; de: 75.0
Investimento em tier 4 Plank &#233; de: 60.0
Investimento em Chedar logs &#233; de: 450.0
value of totaT5 is 6.0

huangapple
  • 本文由 发表于 2020年8月18日 22:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63470511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定