[.net] Please Help me check my C# codes...

Started by
3 comments, last by darookie 16 years, 7 months ago
I met some problem with the code in below. Please help me how to solve it Thanks! =============================================

class myGame
{
    static void Main()
    {
        System.Console.WriteLine("Please Enter Carator's Name:\n-");
        string mName;
        int mSex;
        int mJob;
        mName = System.Console.ReadLine();
        System.Console.WriteLine("Please Select Carator's Sex:\n([1]Male[2]Female)\n-");
        mSex = System.Console.ReadLine();
        System.Console.WriteLine("Please Select Carator's Job:\n([1]Warrior[2]Roger[3]Wizzard)\n-");
        mJob = System.Console.ReadLine();
        if (CaractorCheck == 0)
        {
            Caractor myCaractor = new Caractor(mName, mSex, mJob);
            myCaractor.ShowStatue();
        }
        else
        {
            System.Console.WriteLine("Invaild Information!");
        }
    }

    static int CaractorCheck(int Sex,int Job)
    {
        if (Sex == 1 | 2)
        {
            if (Sex != 1 | 2 | 3)
            {
                return -1;
            }
            return 0;
        }
        else
        {
            return -1;
        }
    }

    class Caractor
    {
        public Caractor(string Name,int Sex,int Job){
            c_Name = Name;
            c_Sex = Sex;
            c_Job = Job;
            MakeNewCaractor(Job, Sex);
            HP = (Str + Dex / 2) * 5;
            MP = (Int + Luk / 2) * 6;
            ATK = (Str + Int) / 2 + 1;
            DEF = (Dex + Luk) / 2 + 1;
        }
        string c_Name;
        int c_Sex, c_Job;
        ////////////// Base Informaion ////////////////
        int Level, Str, Int, Dex, Luk, ATK, DEF, HP, MP;
        string[] myJob = {"Warrio","Roge","Wizzard"};
        char[] mySex = {'M','F'};

        public void ShowStatue()
        {
            System.Console.WriteLine("Carator Statue\nName:{0}    Sex:{1}    Job:{2}",c_Name,mySex[c_Sex],myJob[c_Job]);
            System.Console.WriteLine("HP:{0} MP:{1}  A/D: {2}/{3}",HP,MP,ATK,DEF);
            System.Console.WriteLine("Level - {0}\nStr:{1}\nDex:{2}\nInt:{3}\nLuk:{4}",Level,Str,Int,Dex,Luk); 
        }

        public void MakeNewCaractor(int Job, int Sex)
        {
            switch (Job)
            {
                case 0:
                    Level = 1;
                    Str = 15;
                    Int = 10;
                    Dex = 20;
                    Luk = 12;
                    break;
                case 1:
                    Level = 1;
                    Str = 12;
                    Int = 9;
                    Dex = 25;
                    Luk = 15;
                    break;
                case 2:
                    Level = 1;
                    Str = 10;
                    Int = 25;
                    Dex = 9;
                    Luk = 10;
                    break ;
            }
        }
    }
}
Advertisement
First a word of advice - do not just say "I have a problem here" and throw a bunch of undocumented, error-ridden (e.g. mispelled vars and method names) at us. At least try to isolate the actual error and extract the offending code and describe the problem.
What did you expect the code to do? What does the code differently from what you expected?

However, I presume your error starts here:
static int CaractorCheck(int Sex,int Job)    {        if (Sex == 1 | 2)        {            if (Sex != 1 | 2 | 3)            {                return -1;            }            return 0;        }        else        {            return -1;        }    }

An expression like if (x == y | z) does not evaluate to if x equals y or x equals z. The | operator is the binary "or" and acts more like an addition without carry than like the logical "or", which is the || operator in C#. Also you have to split the expression into sub-expressions, e.g. if x equals y OR x equals z, which translates to
if (x == y || x == z){   // ...}


My advise: check your C# language reference and do some more reading on introductionary material on the C# language.

Good luck!
Pat.
I feel sorry darookie, I will pay more attention on it in future.

I want to generate a simple console program for practicing C#. This part is about create a caractor and if user entered a right number for caracter's Sex and Job the program will output caracter's state in the console.

Through your kindly help I am really clear on how to use an "or" operator. But still there are some problems. For my system language is Simplified Chinese i don't know how to translate them in the right way... I cut those code here and try my best let it clear please help me more. Thanks.

by the way I want to know how to out my codes in an text area just like the style of yours?




Quote: Code Error1: Can't convert "string" into "int";
...
int mSex;
int mJob;
...
mSex = System.Console.ReadLine();
...
mJob = System.Console.ReadLine();
...



Quote:Code Error2: Can not apply operator "==" for "MethodGroup" and "int"
...if (CaractorCheck == 0)        {            Caractor myCaractor = new Caractor(mName, mSex, mJob);            myCaractor.ShowStatue();        }...
Moved to .NET.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Your first problem is caused by the return value of the ReadLine() method. The method tries to read a string from the console, so you need to convert the result to an integer value. Beware that the user input might not be convertible and you need to be prepared for that. You can read about how to do that at MSDN.

Your second error is rather a syntax error. You need to append parentheses to your method identifier in order to signal a method invokation. You also need to pass the required parameters:
if (CaractorCheck(mSex, mJob) == 0){     Caractor myCaractor = new Caractor(mName, mSex, mJob);     myCaractor.ShowStatue();}


You can use the [source] [/source] tags to produce a box containing source code in your posts.

Cheers,
Pat

This topic is closed to new replies.

Advertisement