而循环计算问题

发布于 2025-01-24 21:42:06 字数 1620 浏览 1 评论 0原文

我目前正在制定Java程序(用于学校),该程序在用户进入其帐户的起始余额时打印两个语句。

因此,例如,如果用户输入$ 10,000,将同时打印两个语句。一个人告诉他们,他们的帐户要花多长时间才能达到100,000美元,而另一个达到100万美元。

现在,我有了下面发布的代码,它可以正常运行(它使我的结果接近所需的结果),但是,我遇到的问题是数学本身。我将发布代码并解释:

public static void main(String[] args) {
    int startBalance = 0;
    int storedBalance = 0;
    int years = 0;
    try (Scanner stdln = new Scanner(System.in)) {
        System.out.println("Please enter your starting balance: ");
        startBalance = stdln.nextInt();
    }
    
    while (storedBalance < 100000)
    {   
        storedBalance = startBalance + (storedBalance * 2) ;
        ++years;
    } 
    System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");
    
    while (storedBalance < 1000000)
    {   
        storedBalance = startBalance + (storedBalance * 2);
        ++years;
    } 
    System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");
}

}

因此,从理论上讲,起步平均数应该翻倍,直到达到100,000美元或更多,在这种情况下,计算到超过100,000美元以上的年份,并打印了声明(与第二个相同循环,但有$ 1,000,000)。因此,如果用户输入$ 10,000,则第一个储存变量,loop的储备变量应$ 20k,$ 40k,$ 80k,$ 160k,但使用数学,我目前的数学售价为$ 10k,$ 30k,$ 70k,$ 70k,$ 150k。

我还尝试了:

StoreDalance =(Startbalance * 2) + StoreDelbalance;

而且它有效,但问题是,而不是将金额加倍(10k到20k,20k至40k,40k至80k等),它只是将20K添加到上一个数字,这是有意义的,因为该语句中的逻辑是有意义的Startbalance为10K IS:(10,000 * 2) +存储Blase,因此括号内的逻辑保持恒定,并且不会随着数字的变化而调整,但是随着每个循环,存储的余额都会增加。

因此,我知道缺少某些东西,因为1年级= $ 10K,何时应为20k美元,但是要到达那里所需的年份是正确的。我觉得自己的数学是不正确的,因为我没有将初始价值加倍,而我只是添加了储存量。

任何和所有回应都将受到极大的赞赏。先感谢您。

I'm currently working on a Java program (for school) that prints two statements when a user enters a starting balance for their account.

So for example, if the user inputs $10,000, there will be two statements that print at the same time. One tells them how long it will take for their account to reach $100,000, and the other when it reaches $1,000,000.

Now I have the code, which I'll post below, and it works (it gets me a result that's close to what is required), however, the issue I'm having is with the math itself. I'll post the code and explain:

public static void main(String[] args) {
    int startBalance = 0;
    int storedBalance = 0;
    int years = 0;
    try (Scanner stdln = new Scanner(System.in)) {
        System.out.println("Please enter your starting balance: ");
        startBalance = stdln.nextInt();
    }
    
    while (storedBalance < 100000)
    {   
        storedBalance = startBalance + (storedBalance * 2) ;
        ++years;
    } 
    System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");
    
    while (storedBalance < 1000000)
    {   
        storedBalance = startBalance + (storedBalance * 2);
        ++years;
    } 
    System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");
}

}

So in theory, the startBalance is supposed to double until it reaches $100,000 or more, in which case the years it takes to reach over $100,000 is calculated and the statement gets printed (same with the second While loop but with $1,000,000). So if the user entered $10,000, the storedBalance variable for the first While loop should go $20k, $40k, $80k, $160k, but with the math I currently have it goes $10k, $30k, $70k, $150k.

I've also tried:

storedBalance = (startBalance * 2) + storedBalance;

and it works, but the issue is that instead of doubling the amount (10k to 20k, 20k to 40k, 40k to 80k, etc.) it just adds 20k to the previous number, which makes sense since the logic in that statement if the startBalance is 10k is: (10,000 * 2) + storedBalance, so the logic within the brackets remains constant and doesn't adjust as the number changes but the stored balance increases with every loop.

So I know that something is missing because year 1 = $10k when it should be $20k but the years it takes to get there are correct. I have a feeling my math is incorrect as I'm not doubling the initial value and I'm just adding the storedBalance to it.

Any and all responses are greatly appreciated. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2025-01-31 21:42:06

您应该初始化StoreDalance等于startbalance在段循环外。然后在while-loops内部,您只需将storedlance在每个循环 - 材料(年)上的值加倍,storeDbalance = storeordBalance = storeordBalance * 2;

public static void main(String[] args) {
int startBalance = 0;
int years = 0;
try (Scanner stdln = new Scanner(System.in)) {
    System.out.println("Please enter your starting balance: ");
    startBalance = stdln.nextInt();
}
int storedBalance = startBalance;
while (storedBalance < 100000)
{   
    storedBalance *= 2;
    ++years;
} 
System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");

storedBalance = startBalance;
years = 0;
while (storedBalance < 1000000)
{   
    storedBalance *= 2;
    ++years;
} 
System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");

}

You should initialize the storedBalance equal to startBalance outside the while loop. Then inside the while-loops you simply double the storedBalance value on every loop-iteration (year) with storedBalance = storedBalance * 2;.

public static void main(String[] args) {
int startBalance = 0;
int years = 0;
try (Scanner stdln = new Scanner(System.in)) {
    System.out.println("Please enter your starting balance: ");
    startBalance = stdln.nextInt();
}
int storedBalance = startBalance;
while (storedBalance < 100000)
{   
    storedBalance *= 2;
    ++years;
} 
System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");

storedBalance = startBalance;
years = 0;
while (storedBalance < 1000000)
{   
    storedBalance *= 2;
    ++years;
} 
System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文