This is my code:
这是我的代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication3 {
public static void main(String[] args){
double baseDiamond, baseDiamondPrice, preDiamond, preDiamondPrice, baseDiamondCalc, finalECoins;
Scanner input = new Scanner(System.in);
System.out.print("What is your current Diamond Miner level? ");
preDiamondPrice = input.nextDouble();
System.out.print("Enter what diamondminer level ");
baseDiamondPrice = input.nextDouble();
while (baseDiamondPrice > 0) {
baseDiamondPrice = ((baseDiamondPrice * 500000) + 1000000);
baseDiamondPrice--;
}
while (preDiamondPrice > 0) {
preDiamondPrice = ((preDiamondPrice * 500000) + 1000000);
preDiamondPrice--;
}
baseDiamondCalc = (baseDiamondPrice - preDiamondPrice);
DecimalFormat dRemover = new DecimalFormat("0.#");
System.out.println("You need " +dRemover.format(baseDiamondCalc)+ " ecoins for diamond miner level "+dRemover.format(baseDiamondPrice)+".");
}}
why doesn't it work?
为什么不起作用?
using the equation y = 1,000,000 + (500,000 * n) I want the summation of all values of n less than n including 0. e.g. if n = 3, I want 1,000,000 + (500,000 * 3) + 1,000,000 + (500,000 * 2) + 1,000,000 + (500,000 * 1) = 1,000,000 + (500,000 * 0).
使用等式y = 1,000,000 +(500,000 * n)我希望n的所有值的总和小于n,包括0。如果n = 3,我想要1,000,000 +(500,000 * 3)+ 1,000,000 +(500,000 * 2)+ 1,000,000 +(500,000 * 1)= 1,000,000 +(500,000 * 0)。
As you can see in my code I want this to happen twice and then subtract one of the sums from the other.
正如你在我的代码中看到的那样,我希望它发生两次,然后从另一个中减去其中一个总和。
2 个解决方案
#1
1
Your code will never exit the loop as-is because it is incrementing the loop control variable faster than it decrements.
您的代码将永远不会按原样退出循环,因为它会使循环控制变量的递增速度快于递减。
while (baseDiamondPrice > 0) {
baseDiamondPrice = ((baseDiamondPrice * 500000) + 1000000);
baseDiamondPrice--;
}
If baseDiamondPrice
is less than or equal to zero, you do not enter the loop. If if it is any positive number, then in each loop iteration you add 1000000 and then subtract one. That will never converge to zero and exit the loop. It will also never arrive at your intended sum. You should separate your control variable from your accumulator variable.
如果baseDiamondPrice小于或等于零,则不进入循环。如果它是任何正数,则在每次循环迭代中添加1000000然后减去1。这永远不会收敛到零并退出循环。它也永远不会达到您的预期金额。您应该将控制变量与累加器变量分开。
You can solve this assignment with a for loop with a loop control variable i
for i
from 0 to n and a separate accumulator variable to hold the running sum.
您可以使用for循环解决此赋值,其中循环控制变量i为i,从0到n,以及单独的累加器变量来保存运行总和。
#2
0
Your control logic is incorrect. It will never exit either loop .You can modify it like below for both while loops.
您的控制逻辑不正确。它永远不会退出任何循环。你可以像下面的两个循环一样修改它。
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication3 {
public static void main(String[] args){
double baseDiamond, baseDiamondPrice, preDiamond, preDiamondPrice, baseDiamondCalc, finalECoins;
Scanner input = new Scanner(System.in);
System.out.print("What is your current Diamond Miner level? ");
preDiamondPrice = input.nextDouble();
System.out.print("Enter what diamondminer level ");
baseDiamondPrice = input.nextDouble();
double counter1 =baseDiamondPrice;
while (counter1 > 0) {
baseDiamondPrice = ((baseDiamondPrice * 500000) + 1000000);
counter1--;
}
double counter2 =preDiamondPrice;
while (counter2 > 0) {
preDiamondPrice = ((preDiamondPrice * 500000) + 1000000);
counter2--;
}
baseDiamondCalc = (baseDiamondPrice - preDiamondPrice);
DecimalFormat dRemover = new DecimalFormat("0.#");
System.out.println("You need " +dRemover.format(baseDiamondCalc)+ " ecoins for diamond miner level "+dRemover.format(baseDiamondPrice)+".");
}}
#1
1
Your code will never exit the loop as-is because it is incrementing the loop control variable faster than it decrements.
您的代码将永远不会按原样退出循环,因为它会使循环控制变量的递增速度快于递减。
while (baseDiamondPrice > 0) {
baseDiamondPrice = ((baseDiamondPrice * 500000) + 1000000);
baseDiamondPrice--;
}
If baseDiamondPrice
is less than or equal to zero, you do not enter the loop. If if it is any positive number, then in each loop iteration you add 1000000 and then subtract one. That will never converge to zero and exit the loop. It will also never arrive at your intended sum. You should separate your control variable from your accumulator variable.
如果baseDiamondPrice小于或等于零,则不进入循环。如果它是任何正数,则在每次循环迭代中添加1000000然后减去1。这永远不会收敛到零并退出循环。它也永远不会达到您的预期金额。您应该将控制变量与累加器变量分开。
You can solve this assignment with a for loop with a loop control variable i
for i
from 0 to n and a separate accumulator variable to hold the running sum.
您可以使用for循环解决此赋值,其中循环控制变量i为i,从0到n,以及单独的累加器变量来保存运行总和。
#2
0
Your control logic is incorrect. It will never exit either loop .You can modify it like below for both while loops.
您的控制逻辑不正确。它永远不会退出任何循环。你可以像下面的两个循环一样修改它。
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication3 {
public static void main(String[] args){
double baseDiamond, baseDiamondPrice, preDiamond, preDiamondPrice, baseDiamondCalc, finalECoins;
Scanner input = new Scanner(System.in);
System.out.print("What is your current Diamond Miner level? ");
preDiamondPrice = input.nextDouble();
System.out.print("Enter what diamondminer level ");
baseDiamondPrice = input.nextDouble();
double counter1 =baseDiamondPrice;
while (counter1 > 0) {
baseDiamondPrice = ((baseDiamondPrice * 500000) + 1000000);
counter1--;
}
double counter2 =preDiamondPrice;
while (counter2 > 0) {
preDiamondPrice = ((preDiamondPrice * 500000) + 1000000);
counter2--;
}
baseDiamondCalc = (baseDiamondPrice - preDiamondPrice);
DecimalFormat dRemover = new DecimalFormat("0.#");
System.out.println("You need " +dRemover.format(baseDiamondCalc)+ " ecoins for diamond miner level "+dRemover.format(baseDiamondPrice)+".");
}}