英文:
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't post but its from the **Scanner** class and is used for the "userInput".
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ç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("value of totaT5 is " +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 >= 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("value of totaT5 is " +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("value of totaT5 is " +oco2.totalT5);
CalculateThings.loopT5();
With below two lines.
oco2.loopT5();
System.out.println("value of totaT5 is " +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("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));
oco2.loopT5(5);
System.out.println("value of totaT5 is " + oco2.totalT5);
}
}
Output:
Investimento em tier 5 Plank é de: 75.0
Investimento em tier 4 Plank é de: 60.0
Investimento em Chedar logs é de: 450.0
value of totaT5 is 6.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论