简体   繁体   中英

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

Loop makes only a single iteration. It gives me 105.0 every single time, and it's supposed to do more iterations than just a single one. 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 (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. Think of a while loop as though it is a for loop.

in a for loop we have three important elements the initial controlling variable, a limiter, and a incrementer.

We have the same structure in while loops. However, a while loop unpacks these things.

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. 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. 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. 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. Pro tip, do-while loops are utterly pointless in the vast majority of cases.

For Loop Example:

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:

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

Edit Answer: I see that you do have your conteggio declared outside. 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. 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. The result in saldoAnnuale is exactly what it would be if you only did it once.

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 += :

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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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