循环或条件语句? [英] Loop or conditional statement?

查看:65
本文介绍了循环或条件语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//You only have to update the 2 here and the rest of the code will function based on the size you set this to
double[] myLottoNums = new double[6];

double sum = 0;
double mean = 0;
double upperVariable = 0;


Console.WriteLine("Please enter numbers: ");

for (int i = 0; i < myLottoNums.Count(); i++)
{
    Console.Write("Number {0}: ", i + 1);
    myLottoNums[i] = double.Parse(Console.ReadLine());
    sum = myLottoNums[i] + sum ;

    if (myLottoNums[i] < 10)
    {
        Console.WriteLine("Sorry that is too low");
    }


}

//Calculate the mean after totalling up the sum
mean = sum / myLottoNums.Count();


//Loop back through our numbers to calculate the upper of each and total it to the upperVariable.
foreach (double d in myLottoNums)
{
    Console.WriteLine("  {0}", d);
    //Console.WriteLine(d.ToString() + " - " + mean.ToString() + " = (" + (d - mean).ToString() + ")");
    // Calculate the value of d by squaring it, then adding it to upperVariable which initializes at 0 but once the first calculation is made, it keeps adding
    upperVariable = (d - mean) * (d - mean) + upperVariable;
}

Console.WriteLine("Thank you. The numbers are: ");

foreach (double x in myLottoNums)
{
    Console.WriteLine("{0}", x);
}
int divisor = myLottoNums.Count() - 1;

Console.WriteLine("The sum is {0}", sum);
Console.WriteLine("The mean is {0}", mean);
Console.WriteLine("The standard deviation is {0}", upperVariable/(divisor));

Console.ReadLine();







大家好问这个逻辑。我正在尝试让我的程序执行以下操作。

如果输入的数字低于10,则让程序再次询问第一个数字。现在,如果我给出一个低于10的数字,它将显示该数字太低但它不会再循环,而是它将继续到第二个值。



任何能引导我朝正确方向前进的建议?



谢谢。




Hey guys question on this logic. I am trying to make my program do the following.
if a number is entered thats below 10 then make the program ask for the first number again. Right now if I give a number below ten it will display that the number is too low but it wont loop again but instead it will go on to the second value.

Any suggestions that can lead me in the right direction?

Thank you.

推荐答案

循环还存在一些其他问题。试试这个:

There are some other problems with the loop as well. Try this:
// arrays have Length property, use that instead of LINQ Count()
for (int i = 0; i < myLottoNums.Length; ) // only increment i for valid numbers
{
    Console.Write("Number {0}: ", i + 1);
    var number = double.Parse(Console.ReadLine());  // read to a variable
	
	if (number >= 10)
	{
		// assign to the array and move to next number only if this one is ok
		myLottoNums[i++] = number; 
		sum += number;  // only update the sum with valid numbers!
	}
	else
    {
        Console.WriteLine("Sorry that is too low");
    }
}


这篇关于循环或条件语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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