java - 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;
}
解决方案
删除break;
你的 for 循环。
注意:添加break
循环将强制循环终止,因此不会让循环上的其他操作继续进行。
推荐阅读
- github - 如何使用graphql按更新日期过滤github拉取请求?
- mysql - Oracle同步中的Varchar(20字节)到Mysql varchar(20)?
- c - 读取文件时使用 fgets() 忽略换行符
- javascript - TextInput 样式问题 - CSS - React Native
- python - 在 OpenCV 中用轴绘制图像
- analytics - Amplitude & Segment.io - Saas 产品分析 - 如何获得所有用户
- python - 程序不将字符串与文本文件中的单词匹配
- ruby-on-rails - 期望同一类的多个 Rails 活动作业以不同的参数排队
- android - 如何使用 logb 将 android 数学函数转换为 swift
- python - 使用正则表达式替换文件中的子字符串和python