[英]Why is my while loop printing out the same thing each time?
對不起,我知道這個問題每天可能會被問一百萬次,但我真的找不到我正在尋找的答案。 我是 Java 的初學者(我正在上大學,正在學習一堆新語言),我的 while 循環每次都打印出同樣的東西。
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("What is the loan amount? ");
int amount = scanner.nextInt();
int x = 1;
//your code goes here
while (x < 6){
System.out.println("Month " +x+ ":");
int percent = (amount / 10);
System.out.println("Payment: 10 percent of " +amount+ " = " +percent);
int rAmt = amount - percent;
System.out.println("Remaining amount: " +rAmt);
x++;
}
}
}
所以問題是,在 while 循環中進行計算后,您永遠不會真正改變amount
。 我認為你想要做的是設置amount = rAmt;
,這將產生以下代碼。 這將導致每次迭代減少 10% 的數量,並將這個新值結轉。
...
//your code goes here
while (x < 6){
System.out.println("Month " +x+ ":");
int percent = (amount / 10);
System.out.println("Payment: 10 percent of " +amount+ " = " +percent);
int rAmt = amount - percent;
System.out.println("Remaining amount: " +rAmt);
amount = rAmt;
x++;
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.