1

我一直在 C# 中搞乱了一点,我偶然发现了一个我没有设法找到答案的问题。我从另一个 .cs 文件在 main 中创建了两个对象,我想为与另一个类中的对象关联的变量分配一个值。看了大约一个小时后,我不知道该怎么做。这是我的代码(我知道它并不完美;我刚刚开始学习):

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

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

        Subject subject1 = new Subject();
        Subject subject2 = new Subject();

        Generate.generation();

    }
}
}

然后

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

namespace Converse
{
public class Subject
{
    public int persuasion;

    public Subject()
    {
        persuasion = 0;
    }
}
}

最后一个问题是第 17 行和第 19 行:

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

namespace Converse
{
public static class Generate
{
   public static void generation()
   {
       Console.WriteLine("Control over Subject  1's personality? ");
       Console.WriteLine("1) None");
       Console.WriteLine("2) Minimal");
       Console.WriteLine("3) Considerable");

       subject1.persuasion = Console.ReadKey();

       switch (subject1.persuasion)
       {
           case 1:
               break;

           case 2:
               GenerationFunctions.lightGen();
               break;

           case 3:
               GenerationFunctions.heavyGen();
               break;
           default:
               break;
       }


       Console.ReadKey();
   }
}

}

谁能告诉我如何正确编码?

谢谢

4

1 回答 1

1

您的代码存在多个问题。您需要将对主题的引用传递给生成无效。

public static void generation(Subject subject)
{
   Console.WriteLine("Control over Subject  1's personality? ");
   Console.WriteLine("1) None");
   Console.WriteLine("2) Minimal");
   Console.WriteLine("3) Considerable");

   subject.persuasion = Convert.ToInt32(Console.ReadKey());

   switch (subject.persuasion)
   {
       case 1:
           break;

       case 2:
           GenerationFunctions.lightGen();
           break;

       case 3:
           GenerationFunctions.heavyGen();
           break;
       default:
           break;
   }
}

然后调用:

Subject subject1 = new Subject();

Generate.generation(subject1);
于 2012-10-16T03:59:00.697 回答