Java 为什么我的while循环每次都打印相同的内容?

Java 为什么我的while循环每次都打印相同的内容?,java,loops,while-loop,Java,Loops,While Loop,对不起,我知道这个问题每天可能被问上一百万次,但我真的找不到我想要的答案。我是Java的初学者(我在上大学,学习了很多新语言),我的while循环每次都在打印相同的东西 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.prin

对不起,我知道这个问题每天可能被问上一百万次,但我真的找不到我想要的答案。我是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++;
        }
    }
}
import java.util.Scanner;
公共班机
{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“贷款金额是多少?”);
int amount=scanner.nextInt();
int x=1;
//你的密码在这里
而(x<6){
System.out.println(“月”+x+“:”);
整数百分比=(金额/10);
系统输出打印项次(“付款:金额+金额+的10%=”+百分比);
int rAmt=金额-百分比;
系统输出打印项次(“剩余金额:+rAmt”);
x++;
}
}
}

因此问题在于,在while循环中进行计算后,您永远不会实际更改
金额。我认为您要做的是设置
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++;
        }
...
。。。
//你的密码在这里
而(x<6){
System.out.println(“月”+x+“:”);
整数百分比=(金额/10);
系统输出打印项次(“付款:金额+金额+的10%=”+百分比);
int rAmt=金额-百分比;
系统输出打印项次(“剩余金额:+rAmt”);
金额=拉姆特;
x++;
}
...

您希望有什么不同?什么是更新金额变量?使用IDE调试器逐步检查代码。如果需要,实际上可以删除
rAmt
变量,并指定
amount=amount-percent,但请确保同时更新打印语句。