简体   繁体   English

为什么while、do-while 和for 循环不起作用?

[英]Why while, do-while and for loops are not working?

Loop makes only a single iteration. Loop 只进行一次迭代。 It gives me 105.0 every single time, and it's supposed to do more iterations than just a single one.它每次都给我 105.0,而且它应该做更多的迭代而不是单次迭代。 Also, I need to write more details because "my post is mostly code", but I can't say anything else because there's anything else to say, I guess.另外,我需要写更多的细节,因为“我的帖子主要是代码”,但我不能说其他任何东西,因为我想还有什么要说的。

import java.util.Scanner;

public class Esercizio {

    public static void main(String[] args) {

        int giorniDieciAnni, mesiDieciAnni, anniDieciAnni;
        giorniDieciAnni = 365*10;
        mesiDieciAnni = 12*10;
        anniDieciAnni = 10;

        System.out.println("");
        System.out.println("Interessatoio!");
        System.out.println("");
        System.out.println("Scrivi un saldo, un interesse, e calcolero':");
        System.out.println("");
        System.out.println("Interesse annuale: una volta l'anno, per 10 anni.");
        System.out.println("Interesse mensile: una volta al mese, per 10 anni.");
        System.out.println("Interesse giornaliero: una volta al giorno per 10 anni.");
        System.out.println("");
        System.out.println("Scrivi un importo per il saldo: massimo due cifre decimali.");
        System.out.println("");

        Scanner tastiera = new Scanner(System.in);
        double saldo, interesse, saldoAnnuale = 0, valoreInteresse = 0;
        saldo = tastiera.nextDouble();

        System.out.println("");
        System.out.println("Inserire ora un tasso di interesse: massimo due cifre decimali.");
        System.out.println("");

        interesse = tastiera.nextDouble();
        valoreInteresse = ((saldo/100)*interesse);
        int conteggio = 1;

        do {
            saldoAnnuale = (saldo + valoreInteresse); 
            conteggio++;
            } while (conteggio <= 10);

        System.out.println("Saldo Annuale Prova: " + saldoAnnuale);


    }
}

your for loop needs to look like this:您的 for 循环需要如下所示:

for (int count = 0; count <= 10; count++) {
doSomething;  }

As for both of your while loops, you need to make sure that you declare a variable outside of the loop.至于你的两个 while 循环,你需要确保你在循环之外声明了一个变量。 Think of a while loop as though it is a for loop.将 while 循环视为 for 循环。

in a for loop we have three important elements the initial controlling variable, a limiter, and a incrementer.在 for 循环中,我们有三个重要元素:初始控制变量、限制器和增量器。

We have the same structure in while loops.我们在 while 循环中有相同的结构。 However, a while loop unpacks these things.然而,while 循环会解包这些东西。

int variable = 0;
while(variable < 10)
{
//10 is the controller here
doSomething();
}

Edit: I would highly recommend that, when you are writing code, write it inside of an IDE such as Eclipse or IntelliJ.编辑:我强烈建议您在编写代码时将其编写在 IDE 中,例如 Eclipse 或 IntelliJ。 Do not listen to what teachers or other folk who tell you about how vital it is to write code inside of a text editor like vi/vim.不要听老师或其他人告诉你在像 vi/vim 这样的文本编辑器中编写代码是多么重要的事情。 It is dumb, pointless, and won't teach you anything.这是愚蠢的,毫无意义的,不会教你任何东西。 If you were to use an IDE you would have found out the error right away.如果您使用 IDE,您会立即发现错误。 Also, make sure you understand how loop structures work.此外,请确保您了解循环结构的工作原理。

Edit 2: The following view examples are of functional for/while loops that I have written both during work and for fun.编辑 2:以下视图示例是我在工作和娱乐期间编写的功能性 for/while 循环。 Pro tip, do-while loops are utterly pointless in the vast majority of cases.专业提示,do-while 循环在绝大多数情况下完全没有意义。

For Loop Example: For 循环示例:

for(int j = 0; j < 5; j++)
                          {
         //TODO: CREATE PROPER LOGIC GATES HERE
         if(rows.get(j).getAttribute("name").contains("P") && currentTime.contains("P"))
                 {
                            logger.info("Both times are still PM.");
                                     assert Integer.parseInt(rows.get(j).getAttribute("name").substring(0, 2).trim()) > currentTimeInt;
                 }
                 else if(rows.get(j).getAttribute("name").contains("A") && currentTime.contains("P"))
                    {
                                     logger.info("Time at index " + j + " is now AM compared to our current PM time which is: " + parseRows.get(3).getAttribute("name"));
                                     assert true;
                     }
             }

Formating is kinda off格式化有点不对

While Loop Example: While 循环示例:

counter = 0;
while(counter < 10)
{
   counter++;
   System.out.println(counter);
}

Edit Answer: I see that you do have your conteggio declared outside.编辑答案:我看到您确实在外面声明了您的 conteggio。 However, the reason your thing prints only once is becauseL ** IT IS OUTSIDE OF THE LOOP** So your loop is fine, it is how you call the Saldo Annuale that's different.然而,你的东西只打印一次的原因是因为L ** 它在循环之外** 所以你的循环很好,这就是你对 Saldo Annuale 的称呼不同。 if you want to be called on each iteration then you need to do the following:如果你想在每次迭代时被调用,那么你需要执行以下操作:

do {
            saldoAnnuale += (saldo + valoreInteresse); 
            conteggio++;
            System.out.println("Saldo Annuale Prova: " + saldoAnnuale);
    } while (conteggio <= 10);

The problem isn't the loop, it's what's inside the loop:问题不在于循环,而在于循环内部的内容:

int conteggio = 1;
do {
    saldoAnnuale = (saldo + valoreInteresse); 
    conteggio++;
    } while (conteggio <= 10);

All that does is the same assignment 10 times.所做的只是相同的任务 10 次。 The result in saldoAnnuale is exactly what it would be if you only did it once. saldoAnnuale的结果正是您只执行一次时的结果。

It's unclear what you want to do, but if (for instance) you wanted to add saldo + valoreInteresse to saldoAnnuale ten times, you'd want += :目前还不清楚你想做什么,但如果(例如)你想saldo + valoreInteresse添加saldoAnnuale十次,你会想要+=

saldoAnnuale += (saldo + valoreInteresse); 
// ----------^

...which is the same as: ...与以下内容相同:

saldoAnnuale = saldoAnnuale + (saldo + valoreInteresse); 

Of course, you could just not loop at all and use:当然,您可以根本不循环并使用:

saldoAnnuale = (saldo + valoreInteresse) * 10; 

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

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