我是编程的初学者,不久前就开始使用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;
}
}
}
}
}
最佳答案
我根据大家提供的所有有用代码提出了一个解决方案。
这就是我的想法,它的工作原理与我想要的一样:
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);
}
}
}
}
}
关于c# - C#回到程序的开头,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24775805/