[英]Why while, do-while and for loops are not working?
Loop 只進行一次迭代。 它每次都給我 105.0,而且它應該做更多的迭代而不是單次迭代。 另外,我需要寫更多的細節,因為“我的帖子主要是代碼”,但我不能說其他任何東西,因為我想還有什么要說的。
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);
}
}
您的 for 循環需要如下所示:
for (int count = 0; count <= 10; count++) {
doSomething; }
至於你的兩個 while 循環,你需要確保你在循環之外聲明了一個變量。 將 while 循環視為 for 循環。
在 for 循環中,我們有三個重要元素:初始控制變量、限制器和增量器。
我們在 while 循環中有相同的結構。 然而,while 循環會解包這些東西。
int variable = 0;
while(variable < 10)
{
//10 is the controller here
doSomething();
}
編輯:我強烈建議您在編寫代碼時將其編寫在 IDE 中,例如 Eclipse 或 IntelliJ。 不要聽老師或其他人告訴你在像 vi/vim 這樣的文本編輯器中編寫代碼是多么重要的事情。 這是愚蠢的,毫無意義的,不會教你任何東西。 如果您使用 IDE,您會立即發現錯誤。 此外,請確保您了解循環結構的工作原理。
編輯 2:以下視圖示例是我在工作和娛樂期間編寫的功能性 for/while 循環。 專業提示,do-while 循環在絕大多數情況下完全沒有意義。
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;
}
}
格式化有點不對
While 循環示例:
counter = 0;
while(counter < 10)
{
counter++;
System.out.println(counter);
}
編輯答案:我看到您確實在外面聲明了您的 conteggio。 然而,你的東西只打印一次的原因是因為L ** 它在循環之外** 所以你的循環很好,這就是你對 Saldo Annuale 的稱呼不同。 如果你想在每次迭代時被調用,那么你需要執行以下操作:
do {
saldoAnnuale += (saldo + valoreInteresse);
conteggio++;
System.out.println("Saldo Annuale Prova: " + saldoAnnuale);
} while (conteggio <= 10);
問題不在於循環,而在於循環內部的內容:
int conteggio = 1;
do {
saldoAnnuale = (saldo + valoreInteresse);
conteggio++;
} while (conteggio <= 10);
所做的只是相同的任務 10 次。 saldoAnnuale
的結果正是您只執行一次時的結果。
目前還不清楚你想做什么,但如果(例如)你想將saldo + valoreInteresse
添加到saldoAnnuale
十次,你會想要+=
:
saldoAnnuale += (saldo + valoreInteresse);
// ----------^
...與以下內容相同:
saldoAnnuale = saldoAnnuale + (saldo + valoreInteresse);
當然,您可以根本不循環並使用:
saldoAnnuale = (saldo + valoreInteresse) * 10;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.