C# 添加每个双位数数字的位数
这是我的密码。我想让用户连续输入任意数量的双倍,直到输入100次(可以是用户想要的次数,但不能超过100次)。显示输入的所有值的总和。在我的代码中,我不知道如何允许用户连续输入数字(我猜您会有一个while循环)。 非常感谢C# 添加每个双位数数字的位数,c#,loops,sum,digits,C#,Loops,Sum,Digits,这是我的密码。我想让用户连续输入任意数量的双倍,直到输入100次(可以是用户想要的次数,但不能超过100次)。显示输入的所有值的总和。在我的代码中,我不知道如何允许用户连续输入数字(我猜您会有一个while循环)。 非常感谢 Console.WriteLine("Enter double"); double.first = double.Parse(Console.ReadLine()); while(first != 0) { Console.Writ
Console.WriteLine("Enter double");
double.first = double.Parse(Console.ReadLine());
while(first != 0)
{
Console.WriteLine("Enter double");
int num = int.Parse(Console.ReadLine());
double sum;
while(num != 0)
{
double ten = num/10;
double tenth = Math.Floor(ten);
double oneth = num % 10;
sum = tenth + oneth;
Console.WriteLine("{0}", sum);
break;
}
first = double.Parse(Console.ReadLine());
}
评论后编辑:
好的,我仍然认为这是家庭作业,但这里是基本的
我会分两个阶段来做,数据输入,然后计算
创建一些东西来存储循环的输入和计数器
var inputs = new List<double>();
var counter = 0;
现在显示总计的值
请记住,有很多方法可以做到这一点。这只是一个例子,可以帮助您解决获取数据的问题
我想让用户连续输入任意数量的双倍,直到
100次(可能是用户想要的次数,但可能更少
超过100)
你需要记住三件事
while循环
。在这个循环中,您应该检查两件事:
完整示例:
static void Main()
{
双重运行总计=0;
加倍下一步;
var迭代次数=0;
控制台。写入(“输入双精度:”;
while(double.TryParse(Console.ReadLine(),out next)&&next!=0&&iterations<100)
{
runningTotal+=下一个;
迭代++;
控制台。写入(“输入双精度:”;
}
WriteLine(“您输入了{0}个数字,给出了{1}的总值”,迭代次数+1,runningTotal);
Console.Read();
}
这是家庭作业吗?不清楚你在问什么。当我读到“显示单独和一起输入的所有值的总和”时,我认为您希望将所有输入的双精度(一起)相加,并且希望将每个输入的双精度(单独)的数字相加。这就是你要问的吗?
while(counter < 100)
{
var tempInput = 0.0D;
Double.TryParse(Console.ReadLine(), out tempInput);
if(tempInput == 0.0D)
{
// The user did not enter something that can be parsed into a double
// If you'd like to use that as the signal that the user is finished entering data,
// just do a break here to exit the loop early
break;
}
inputs.Add(tempInput);
// This is your limiter, once counter reaches 100 the loop will exit on its own
counter++;
}
var total = 0.0D;
foreach(var value in inputs)
{
total += value;
}
double next;
double runningTotal = 0;
int iterations = 0;
while (double.TryParse(Console.ReadLine(), out next) && next != 0 && iterations < 100) {
// Count number of inputs.
iterations++;
// Add to the running total.
runningTotal += next;
}
Console.WriteLine("You entered {0} number(s) giving a total value of {1}", iterations+1, runningTotal);
static void Main()
{
double runningTotal = 0;
double next;
var iterations = 0;
Console.Write("Enter double: ");
while (double.TryParse(Console.ReadLine(), out next) && next != 0 && iterations < 100)
{
runningTotal += next;
iterations++;
Console.Write("Enter double: ");
}
Console.WriteLine("You entered {0} number(s) giving a total value of {1}", iterations+1, runningTotal);
Console.Read();
}