2013-10-28 186 views
-4

我正在寫我的第一個C#程序(FizzBu​​zz),它必須顯示9和13的整除以及打印素數。我認爲我已經將大部分代碼分類出來了,但無法解決我的11'無效標記'和'標識符預期'錯誤。任何意見,將不勝感激。 C#無效語法錯誤

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; } }
+2

首先,你爲什麼要在一個函數內部創建'public'變量?這對你沒有任何好處。 – gunr2171

+0

而你的函數沒有啓動'{'。將'BeginTesting()'後面的';'替換爲'{',然後在末尾做一個。但似乎你需要拿起一本關於c#的書,並通讀前幾章。 – gunr2171

回答

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); 
     } 
    } 

你的代碼有很多問題。我希望這至少會給你一些指示。

+0

非常感謝,看到錯誤數量下降讓我的夜晚 – RebukeJoy

0

這是錯誤的代碼

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

我< = ????

+0

這不是他的代碼唯一的錯誤。 – gunr2171

+0

雅我甚至沒有看到丟失的括號。 – deathismyfriend

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; 
     } 


    } 
} 

編輯:我編譯後提交,只是爲了確保。清理最後的方法。

+1

提示:請在您提出答案之前編譯並格式化您的代碼。 OP的問題是代碼不能編譯。 – gunr2171

+1

好點,@ gunr2171。對不起,我有點匆忙。編譯並測試它,並在其上進行一些小的調整。它現在有它的魔力。 欣賞這裏誠實的地方。 :) – napo