C#回到程序的开头

时间:2014-07-16 08:26:59

标签: c# if-statement error-handling

我是编程的初学者,不久前我开始使用C#。我的问题是,如果用户在第二个插槽中输入较小的数字,我该如何回到这个小程序的开头?

这是代码:

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

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



        Console.Write("Starting number?     ->    ");
        int number1 = int.Parse(Console.ReadLine());


        Console.Write("Ending number?     ->    ");
        int number2 = int.Parse(Console.ReadLine());


        if (number1 >= number2)
        {
        Console.WriteLine("Starting number has to be smaller than the ending number!");

        }

        var number = new Queue<int>();
        number.Enqueue(9999);
        int fizzorbuzz = number.Dequeue();



        for (int i = number1; i < number2; i++)
        {
            number.Enqueue(i);
        }

        foreach (int i in number)
        {
            if (i % 3 == 0 && i % 5 == 0)
            {
                Console.WriteLine("FIZZBUZZZ");
            }
            else if (i % 5 == 0)
            {
                Console.WriteLine("Buzz!");
            }
            else if (i % 3 == 0)
            {
                Console.WriteLine("Fizz!");
            }
            else
            {
                Console.WriteLine(i);
                break;
            }
          }
        }
    }
  }

4 个答案:

答案 0 :(得分:1)

我想出了一个基于我从你们那里得到的所有有用代码的解决方案。

这就是我想到的,它就像我想要的那样:

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

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

        int smallerNumber = 0;
        int biggerNumber = 0;

        bool running = true;

        while (running)
        {
            Console.Write("Starting number?     ->    "); 
            smallerNumber = int.Parse(Console.ReadLine());


            Console.Write("Ending number?       ->    ");
            biggerNumber = int.Parse(Console.ReadLine());


            if (smallerNumber >= biggerNumber)
            {
       Console.WriteLine("Starting number has to be smaller than the ending number!");

            }
            else
            {
                running = false;
            }
        }

        var number = new Queue<int>();
        number.Enqueue(9999);
        int fizzorbuzz = number.Dequeue();
        for (int i = smallerNumber; i < biggerNumber; i++)
        {
            number.Enqueue(i);
        }
        foreach (int i in number)
        {
            if (i % 3 == 0 && i % 5 == 0)
            {
                Console.WriteLine("FIZZBUZZZ");
            }
            else if (i % 5 == 0)
            {
                Console.WriteLine("Buzz!");
            }
            else if (i % 3 == 0)
            {
                Console.WriteLine("Fizz!");
            }
            else
            {
                Console.WriteLine(i);
            }
        }
    }
}
}

答案 1 :(得分:0)

int number1;
int number2;
do{    
Console.Write("Starting number?     ->    ");
number1 = int.Parse(Console.ReadLine());            

Console.Write("Ending number?     ->    ");
number2 = int.Parse(Console.ReadLine());

if (number1 >= number2)
{
  Console.WriteLine("Starting number has to be smaller than the ending number!");
}                
} 
while (number1 >= number2);        

答案 2 :(得分:0)

除了简单的while循环外,还要考虑这个可重用的场景

static void Main(string[] args)
{
    while(DoJob()) {}
}

static bool DoJob()
{
    ...
        return true; // want restart
    ...
    return false; // want exit
}

答案 3 :(得分:-1)

enter image description here您可以使用类似do..while循环的内容,如下所示: -

    int number1, number2;
   do{    
        Console.Write("Starting number?     ->    ");
        number1 = int.Parse(Console.ReadLine());


        Console.Write("Ending number?     ->    ");
        number2 = int.Parse(Console.ReadLine());

   }while (number1 <= number2);
    {
        Console.WriteLine("Starting number has to be smaller than the ending number!");
    }