如何返回for循环的开始?

时间:2015-04-02 15:51:28

标签: c#

我正在解决下面描述的问题,我正在使用循环来解决它。我还没上课。如果输入的数据不正确,我需要重新启动循环。

在If语句中,

if (isInteger == false) 

我可以使用break语句来转义,但for循环的其余部分将执行,它将重新启动并递增到i = 1.如果数据类型错误,我需要它回到开始循环,其中i = 0.我该怎么做?

挑战:编写一个程序,从用户读取5个整数并打印它们的总和。如果输入的号码无效,程序应提示用户输入另一个号码。

Console.WriteLine("This program accepts 5 integers from the user and returns their sum.\nIf an invalid integer is entered, the program notifies the user and asks for correct input.");

        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Please enter Integer {0} now.", (i + 1));
            string rawInput = Console.ReadLine();

            int integerSum = 0;

            int integerInput;
            bool isInteger = int.TryParse(rawInput, out integerInput);

            if (isInteger == false)
            {
                Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");
                break;
            }
            else
            {                   
                integerSum += integerInput;                   
            }
        }

7 个答案:

答案 0 :(得分:4)

像这样继续使用

if (isInteger == false)
{
   Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");
   i--;
   continue;
}

答案 1 :(得分:3)

这应该这样做:

public class Program
{
    static void Main()
    {
        int sum = 0, value = 0;

        for (int i = 1; i <= 5; i++)
        {
            Console.WriteLine("Please enter Integer {0} now.", i);

            while (!int.TryParse(Console.ReadLine(), out value))
            {
                Console.WriteLine("This is not a valid integer. Please enter a valid integer {0} now:", i);
            }

            sum += value;
        }

        Console.WriteLine(sum);

    }
}

答案 2 :(得分:2)

查看c#关键字

  

继续

此外,您还有一个声明

的错误

int integerSum = 0;

在循环范围内移动循环,否则你不会对数字求和。

    Console.WriteLine("This program accepts 5 integers from the user and returns their sum.\nIf an invalid integer is entered, the program notifies the user and asks for correct input.");

    int integerSum = 0;

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine("Please enter Integer {0} now.", (i + 1));
        string rawInput = Console.ReadLine();



        int integerInput;
        bool isInteger = int.TryParse(rawInput, out integerInput);

        if (isInteger == false)
        {
            Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");
            i--;                
            continue;
        }
        else
        {                   
            integerSum += integerInput;                   
        }
    }

    Console.WriteLine("Result: " + integerSum);

以下是有关继续的详细信息:

  

continue语句将控制权传递给封闭的while,do,for或foreach语句的下一次迭代。

class ContinueTest
{
 static void Main()
 {
    for (int i = 1; i <= 10; i++)
    {
        if (i < 9)
        {
            continue;
        }
        Console.WriteLine(i);
    }

    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
   }
}
/*
Output:
9
10
*/

MSDN further details

答案 3 :(得分:2)

您不应该只使用continue返回循环的顶部,因为您最终会减少迭代次数。

你有几个选择:

  1. 减少i并使用continue
  2. 从循环标题中删除i++,然后将其移动到循环体的末尾,或
  3. 执行#2,并切换到while循环以更清晰。
  4. 前两个选择将代码转换为for循环。我认为while循环会更好:

    int i = 0;
    int integerSum = 0;
    while (i != 5) {
         Console.WriteLine("Please enter Integer {0} now.", (i + 1));
         string rawInput = Console.ReadLine();
         int integerInput;
         if (!int.TryParse(rawInput, out integerInput)) {
             Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");
             continue;
         }
         integerSum += integerInput;                   
         i++;
    }
    

    不要忘记将integerSum移出循环!

答案 4 :(得分:2)

答案!很快你将成为一个超级丰富的l33t编码器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int integerSum = 0;
            int count = 0;
            while (true)
            {
                Console.WriteLine("Please enter Integer {0} now.", (count + 1));
                string rawInput = Console.ReadLine();

                int integerInput;
                bool isInteger = int.TryParse(rawInput, out integerInput);

                if (isInteger == false)
                {
                    Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");
                }
                else
                {
                    integerSum += integerInput;
                    count ++;
                }
                if (count >= 5)
                {
                    break;
                }
            }
            Console.WriteLine("sum = " + integerSum);
        }
    }
}

答案 5 :(得分:2)

目前,您正试图在一次操作中做太多事情。您应该从“从用户获取5个有效值”操作中分离出“从用户获取有效值,无论尝试多少次”。

private static int SumFiveInts()
{
    Console.WriteLine("This program accepts 5 integers from the user and returns their sum.\nIf an invalid integer is entered, the program notifies the user and asks for correct input.");

    int sum = 0;
    for (int i = 0; i < 5; i++)
    {
        sum += GetIntegerFromConsole(i);
    }
    return sum;
}
public static int GetIntegerFromConsole(int attempt)
{
    Console.WriteLine("Please enter Integer {0} now.", (attempt + 1));
    int output;
    while (!int.TryParse(Console.ReadLine(), out output))
        Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");
    return output;
}

答案 6 :(得分:0)

我知道它已经有一段时间且帖子很长,我找到了一种编写此代码的简单方法..

 int integerSum = 0;

        for (int i = 0; i < 5;)
        {
            Console.WriteLine("Please enter Integer {0} now.", (i + 1));
            string rawInput = Console.ReadLine();

            int integerInput;
            bool isInteger = int.TryParse(rawInput, out integerInput);

            if (isInteger == false)
            {
                Console.WriteLine("This is not a valid integer. Please enter a valid integer now:");

            }
            else if (isInteger == true)
            {
                integerSum += integerInput;
                i++;
            }

        }
        Console.WriteLine("The sum of all number entered is:{1}{0} ", integerSum, Environment.NewLine);