2013-02-16 39 views
1

我有下面的代碼,我的問題是,當我引用變量時,它給了我錯誤:對象引用是非靜態字段,方法,或每個變量的屬性。我知道這是與我的公衆詮釋和公共雙重是非靜態的,但我不知道如何解決它。有人能告訴我可能嗎?問題與非靜態字段需要對象引用

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

namespace homework3 
{ 
    public class Program 
    { 

      public int number_of_scores; 
      public double score, total_score = 0, high_score, low_score, average; 


    } 
    class Class1{ 

     static void Main(string[] args){ 


      Console.Write("please enter the number of scores that you 
wish to process? "); 
      Program.number_of_scores = int.Parse(Console.ReadLine()); 

      Console.Write("Please enter score " + 1 + " "); 
      Program.score = double.Parse(Console.ReadLine()); 

      Program.high_score = Program.score; 
      Program.low_score = Program.score; 

      Program.total_score = Program.total_score = Program.score; 


      for (int i = 2; i <= number_of_scores; i++); 
     } 
} 

    class class2 
     { 
     static void Main(string[] args){ 


       Console.Write("Please enter score " + i + " "); 
       Program.score = double.Parse(Console.ReadLine()); 

       Program.total_score = Program.total_score + Program.score; 

       if(Program.score > Program.high_score) 
       Program.high_score = Program.score; 

       if(Program.score < Program.low_score) 
       Program.low_score = Program.score; 
     } 
      } 

     class Class3 
    { 
      static void Main(string[] args){ 

      Program.average = Program.total_score/Program.number_of_scores; 

      Console.WriteLine("you entered " + Program.number_of_scores + 
" scores"); 
      Console.WriteLine("The high score is " + Program.high_score); 
      Console.WriteLine("The low score is " + Program.low_score); 
      Console.WriteLine("the average score is " + Program.average); 
    } 
     } 

回答

1

在行:

Program.number_of_scores = int.Parse(Console.ReadLine()); 

您嘗試從靜態方法引用實例變量number_of_scores

最微不足道的方式來獲取工作是聲明number_of_scores爲靜態:

static public int number_of_scores; 

你的一些其他領域的有同樣的問題。

+1

....我覺得現在啞赫克哈哈哈 – Zi0n1 2013-02-16 03:02:40

+0

學習:-) – 2013-02-16 03:45:16

1

您需要將number_of_scores和score(以及其他變量)聲明爲static。

public static int number_of_scores; 
public static double score, //etc 
+0

的所有部分,我想我要問的是: 如何聲明爲靜態的? 或 你會怎麼做?我似乎無法像其他人那樣做,因爲他們是公開的,他們對我來說很奇怪,我對此不是很熟悉。 – Zi0n1 2013-02-16 03:00:18

+0

@ user2052853更新了答案。 – sparky68967 2013-02-16 03:02:12

相關問題