首页 > 解决方案 > How to make my for-loop run according to the user inputs? (i.e user inputs 2, loop runs 2 times)

问题描述

I am trying to get p to double according to the number of days the user inputs, but right now no matter what I input p does not increase, how do I fix this?

Scanner dayNum = new Scanner(System.in);
System.out.print("Enter a number of days for the Double Yesterday option: ");
int d = dayNum.nextInt();
double p = .01;
double yesterday = 1000000;
String pattern = "#,###,###";
DecimalFormat df = new DecimalFormat(pattern);
System.out.println("The duration of Double Yesterday is " + d);
for (p = .01; p < d; p*=2 )
//while (p > d)
{
    String penny = "$###.00";
    DecimalFormat dg = new DecimalFormat(penny);    
    if (p < yesterday)
    {
        System.out.println("DOUBLE YESTERDAY FOR " + d + " DAYS GIVES MIKE " + dg.format(p) + ", WHICH IS LESS THAN " + df.format(yesterday) + " SO TAKE THE " + df.format(yesterday) + " BYEBYE");
    }
    System.out.println("");
    break;
}

标签: javaloopsfor-loopwhile-loop

解决方案


删除break;你的 for 循环。

注意:添加break循环将强制循环终止,因此不会让循环上的其他操作继续进行。


推荐阅读