C#语法错误无效

时间:2013-10-28 01:47:36

标签: c# .net fizzbuzz

我正在编写我的第一个C#程序(FizzBu​​zz),它必须显示9和13的整除以及打印素数。我想我现在已经对大部分代码进行了排序,但无法理解我所拥有的11'无效令牌'和'标识符预期'错误。对此有任何建议将不胜感激。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using ConsoleApplication10; namespace Fizzbuzz { class Program { int TotalFizz; int TotalBuzz; int TotalFizzBuzz; int IsPrime; public void BeginTesting(); // start testing //Step 1 : Print number 1 to 100 for (int i = 1; i <=; i++) { string results = ""; // Step 2 :- Divisable by 9 print Fizz public bool IsFizz(i % 9 == 0) results = "Fizz"; public int TotalFizz (int input); // Step 3 :- Divisable by 13 print Buzz public bool IsBuzz(i % 13 == 0) results = "Buzz"; public int TotalBuzz(int input); // Step 4 :- Divisable by 9 and 13 print FizzBuzz public bool IsFizzBuzz(i % 9 == 0, i % 13 == 0) results = "FizzBuzz"; public int TotalFizzBuzz(int input); // Step 5 :- Print the number as it is if not divisable by 9 or 13 Console.WriteLine("TotalFizzBuzz")CurrentValue.ToString; public bool IsPrime(int input); Console.WriteLine results; } }

3 个答案:

答案 0 :(得分:1)

为了给你一个基本的开始,这里是你的函数的一个版本,它主要执行评论所建议的。

    public void BeginTesting() // start testing
    {
        //Step 1 : Print number 1 to 100
        for (int i = 1; i <= 100; i++)
        {
            var results = string.Empty;
            // Step 2 :- Divisable by 9 print Fizz
            if (i % 9 == 0)
            {
                results = "Fizz";
                TotalFizz++;
            }

            // Step 3 :- Divisable by 13 print Buzz
            if (i % 13 == 0)
            {
                results = "Buzz";
                TotalBuzz++;
            }

            // Step 4 :- Divisable by 9 and 13 print FizzBuzz
            if (i % 9 == 0 && i % 13 == 0)
            {
                results = "FizzBuzz";
                TotalFizzBuzz++;
            }

            if (string.IsNullOrEmpty(results))
            {
                // Step 5 :- Print the number as it is if not divisable by 9 or 13
                Console.WriteLine(i.ToString());
            }

            Console.WriteLine(results);
        }
    }

您的代码存在很多问题。我希望这至少会给你一些指示。

答案 1 :(得分:0)

你的代码错误

    for (int i = 1; i <=; i++)

我&lt; = ????

答案 2 :(得分:0)

尝试这个(只是编译它,看起来应该这样做)。我抽象了你的号码检查,所以你不必一遍又一遍地做同样的剩余检查。

看起来你的事情过于复杂......但我也一直在那里做过。 :)

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            BeginTesting();
            Console.ReadLine();
        }

        private static void BeginTesting()
        {
            //Step 1 : Print number 1 to 100
            for (int i = 1; i <= 100; i++)
            {

                // Step 2 :- Divisable by 9 print Fizz
                if (isDivisibleBy(i, 9))
                {
                    Console.WriteLine("Fizz");
                }

                // Step 3 :- Divisable by 13 print Buzz
                else if (isDivisibleBy(i, 13))
                {
                    Console.WriteLine("Buzz");
                }

                // Step 4 :- Divisable by 9 and 13 print FizzBuzz
                else if (isDivisibleBy(i, 9) && isDivisibleBy(i, 13))
                {
                    Console.WriteLine("FizzBuzz");
                }

                // Step 5 :- Print the number as it is if not divisable by 9 or 13
                else
                {
                    Console.WriteLine(i);
                }

            }
        }



        //Create a method to test whether it’s divisible
        private static bool isDivisibleBy(int theNumber, int theDivisor)
        {
            bool isDivisible = false;

            if ((theNumber % theDivisor) == 0)
            {
                isDivisible = true;
            }

            return isDivisible;
        }


    }
}

编辑:我提交后编译,只是为了确定。清理了最后一种方法。