循环,同时增加2倍 [英] Looping while increasing by 2x

查看:123
本文介绍了循环,同时增加2倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用c#和visual studio处理作业。我希望用户输入他们工作的天数以及每天在列表框中显示的工资。但是,薪水需要每天翻倍。因此,第1天他们得到1便士,第2天他们得到2便士,第3天,他们得到4便士等。难以增加2倍,同时仍然保持计数仅上升一天。



我尝试过:



//声明变量

double dblDays;

double dblPay;

int intCounter = 1;







if(Double.TryParse(txtDays.Text,out dblDays))

{

for(intCounter = 1; intCounter< ; = dblDays; intCounter ++)

{

dblPay =(dblDays * .01)* intCounter;

lstDays.Items.Add( 支付日+ intCounter.ToString()+=+ dblPay.ToString(c));

lblTotalPayEarnedTotal.Text = dblPay.ToString(c);

Working on an assignment using c# and visual studio. I want the user to type in how many days they worked and the pay for each day to show in a listbox. However, the pay needs to double each day. So day 1 they get 1 penny, day 2 they get 2 pennies, day 3, they get 4 pennies, etc. Having trouble getting to increase by 2x, while still keeping the count only going up by one day.

What I have tried:

//declare the variables
double dblDays;
double dblPay;
int intCounter=1;



if (Double.TryParse(txtDays.Text, out dblDays))
{
for (intCounter = 1; intCounter <= dblDays; intCounter ++)
{
dblPay = (dblDays*.01) * intCounter;
lstDays.Items.Add("Pay for Day" + intCounter.ToString() + "=" + dblPay.ToString("c"));
lblTotalPayEarnedTotal.Text = dblPay.ToString("c");

推荐答案

更改

dblPay =(dblDays * .01)* intCounter;

to

dblPay =(dblDays * .01)* Math.Pow(2,( intCounter-1));



因为在数学中,

2 ^ 0 = 1

2 ^ 1 = 2

2 ^ 2 = 4

2 ^ 3 = 8

等......
change
dblPay = (dblDays*.01) * intCounter;
to
dblPay = (dblDays*.01) * Math.Pow(2,(intCounter-1));

because in math,
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
etc...


这篇关于循环,同时增加2倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆