我需要一个奇怪的C#控制台应用程序循环 [英] I need an odd type of loop for C# console application
问题描述
我试图在学校找到最容易使用的循环作业。我们正在使用C#2012,我需要它在同一行输出一个数字,表示它是哪一天(当天的整数,只需要一个数字 - 1,2,3,4等等)和在每天的数字右边,我需要它输出一个以0.01开头的小数/双倍,每天加倍(所以第2天需要说2然后0.02间距足以让标题显示日期:和量)。最终结果需要打印出类似下面的内容 -
当天欠款
--- -------- ---
1 0.01
2 0.02
3 0.04
需要计算到第64天
到目前为止我有以下代码,但它给了我变量i的错误。
错误1使用未分配的局部变量'i'f:\documents\visual studio 2012 \Projects\RichBeggarBC\RichBeggarBC\Program.cs 18 23 RichBeggarBC
即使我从双i = 0.01中删除= 0.01,也会出现相同的错误。
I am trying to find out the easiest to use loop for an assignment in school. We are using C# 2012 and I need it to on the same line output a number representing which day it is (integer for the day, just needs to be a number - 1, 2, 3, 4, etc going down the lines) and to the right of each day number I need it to put out a decimal/double that starts with 0.01 and doubles each day (so day 2 would need to say 2 and then 0.02 spaced out enough to have a header showing "Day:" and "Amount"). End result needs to print out something similar to the below -
Day Amount Owed
--- -----------
1 0.01
2 0.02
3 0.04
needs to calculate until day 64
I have the below code so far, but it gives me errors for the variable i.
Error 1 Use of unassigned local variable 'i' f:\documents\visual studio 2012\Projects\RichBeggarBC\RichBeggarBC\Program.cs 18 23 RichBeggarBC
Same error is given even if I remove the = 0.01 from the double i = 0.01;.
static void Main(string[] args)
{
//since the output will require a decimal, I am using a double as the variable type to grant more than 7 spaces
double number;
//used int for day
int day = 1;
double i = 0.01;
Console.Write("Day: Amount owed:\n");
//for loop to run until number is made
for (i = 0.01; day <= 64; i *= 2)
Console.WriteLine (day);
//console.readline used to sop program from finishing in order to allow it to remain open
Console.ReadLine();
}
我刚试过一次嵌套for循环并在尝试使用时出错。
I have also just tried a nested for loop and get errors when trying to use it.
for (int i =1; i <= 64; i++)
{
for (double j = 0.01; j * 2)
{
Console.WriteLine (i);
}
Console.WriteLine ();
}
这给了我2个错误。我得到1个错误,说它预期了;在j * 2之后,错误2表示不能将双重变为bool并突出显示j * 2)。我只是想这样做,所以每一行打印i(并增加1)然后j后面,以便它会像原始帖子中的表格一样读取。
This gives me 2 errors. I get 1 error saying it expected a ; after the j * 2) and error 2 says cannot change double into bool and highlights j * 2). I just want to make it so each line prints i (and increment by 1) and then j after it so that it would read like in the table in the original post.
推荐答案
关于第一个循环
Concerning the first loop
for (i = 0.01; day <= 64; i *= 2)
循环的中断条件基于日变量。但是,你根本没有增加这一天。
为什么不循环一天并在循环内增加i。所以循环将是
The break condition for the loop is base on day variable. However, you don't increment the day at all.
Why not loop through the day and increment i inside the loop. So the loop would be
for (day = 1; day <= 64; day++)
第二个循环的内容
What comes to the second loop
for (double j = 0.01; j * 2)
您应该添加条件,循环持续多长时间。循环总是包含三个部分:
You should add the condition, how long the loop is continued. The loop always contains three parts:
for (initializer; condition; iterator)
现在你错过了这个错误的条件
Now you're missing the condition thus the error
问题是我没有看到除了day = 64之外的条件。因此,第二个循环可以 -
for(double j = 0.01; day< = 64; j * 2)
另外,这是否会随着日期的增加而增加,以便第2天= 0.02,第3天= 0.04等?
The issue is that I do not see a condition other than day = 64. Therefore would the 2nd loop be able to be -
for (double j = 0.01; day <= 64; j * 2)
Also, would this keep it incrementing with the day so that day 2 = 0.02, day 3 = 0.04, etc?
只是更新。我进行了以下更改,我让它在没有console.writeline(day)的情况下运行 - 但是它只运行了1天,其余的应用程序没有任何内容,它继续无限。我需要它为第1天添加0.01的总和,并且每天乘以2,在第2天的右边第2个列(大约5-6个空格)。我不确定为什么它当天没有增加到64的限制以及为什么它没有写增量线。
Just an update. I made the below changes and I got it to run without the console.writeline (day) - however it only ran 1 for the day and nothing for the rest of the application and it continued for infinity. I need it to add the total of the 0.01 for day 1 and multiplied by 2 each day for a 2nd "column" to the right of the day number (about 5-6 spaces away). I am not sure why it is not incrementing to a limit of 64 on the day and why it is not writing the line for the increment.
for (day = 1; day <= 64; day++)
{
for (double j = 0.01; day <= 64; j *= 2)
{
Console.WriteLine (day);
}
Console.WriteLine ();
}
这篇关于我需要一个奇怪的C#控制台应用程序循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!