C#初学者问题 [英] C# Beginner question

查看:56
本文介绍了C#初学者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经为基本的Hero vs Monster程序编写了代码。问题是当我运行代码时,英雄攻击,英雄健康,怪物攻击和怪物健康(这意味着一切)的价值不会改变。

So,I have written code for a basic Hero vs Monster program.The problem is when I run the code,the value of hero attack,hero health,monster attack and monster health (that means EVERYTHING) doesn't change.

namespace ConsoleApplication1
{
    class Battle
    {
        public static int heroat, monsterat, herohealth, monsterhealth;
        
        public Battle()
        {
            heroat = 0;
           monsterat=0;
            
            herohealth = 100;
            monsterhealth = 100;
            do
            {
                Console.WriteLine(@" 
            {2} Health={0}               Monster Health={1}
                Hero's Attack={3}        Monster's Attack={4}", herohealth, monsterhealth, Main.heroname,heroat,monsterat);
            
            
                HeroAtk(heroat, herohealth, monsterat, monsterhealth);
                MonsAtk(heroat, herohealth, monsterat, monsterhealth);
               
                Console.ReadLine();
            }
            while (herohealth > 0 && monsterhealth > 0);



        }
        public static void HeroAtk(int heroat,int herohealth,int monsterat,int monsterhealth)
        {Random rand;
            rand=new Random();
            heroat = rand.Next(10, 21);
            monsterhealth=monsterhealth - heroat;
           
        }
        public static void MonsAtk(int heroat, int herohealth, int monsterat, int monsterhealth )
        {
            Random rand;
            rand = new Random();
            monsterat = rand.Next(10, 20);
            herohealth = herohealth - monsterat;
           
        }
        
        }
    }



我做错了什么?我该如何纠正呢?


What am I doing wrong?And how do I correct it?

推荐答案

首先,在需要时停止创建新的Random实例:在班级创建一个:

First off, stop creating new Random instance when you want them: create one at class level:
private Random rand = new Random();

因为随机启动它是使用系统时钟作为启动器的一代,如果你反复创建它们,你每次都可能生成相同的序列!



其次,你需要了解发生了什么将参数传递给方法时:传递的变量是按值传递的,而不是通过引用传递的。这意味着获取变量的副本并传递给方法,该方法可以随意修改它,因为它不会影响方法之外的世界! (有关此内容的更详细说明:使用结构和类 - 什么都有关? [ ^ ]但对初学者来说可能有点太多了。



想想看它:

Because Random starts it's generation using the system clock as a starter, if you create them repeatedly you are likely to generate the same sequence each time!

Secondly, you need to understand what happens when you pass a parameter to a method: the variable you pass is passed by value, not by reference. What that means is that a copy of the variable is taken and passes to the method, which can modify it all it likes, because it will not affect the world outside the method! (There is a more detailed explanation of what happens in this: Using struct and class - what's that all about?[^] but it might be a bit too much for you as a beginner).

Think about it:

void add(int a)
   {
   a = a + 1;
   }

当你这样做时很好和花花公子:

Is fine and dandy when you do this:

int A = 6;
add(A);

但是当你这样做时会发生什么:

But what happens when you do this:

add(6);

您无法开始更改常量! :笑:



你可以通过传递对值的引用来做到这一点 - 这很容易 - 但更好的解决方案是使用当前的类值,而不是传递任何东西:



You can't start changing constants! :laugh:

You could do it by passing a reference to the value - and that's very easy - but a better solution is to use the current class values, and not pass anything:

namespace ConsoleApplication1
{
    class Battle
    {
        public int heroat, monsterat, herohealth, monsterhealth;
        private Random rand = new Random();
        public Battle()
        {
            heroat = 0;
            monsterat=0;
            
            herohealth = 100;
            monsterhealth = 100;
            do
            {
                Console.WriteLine(@" 
            {2} Health={0}               Monster Health={1}
                Hero's Attack={3}        Monster's Attack={4}", herohealth, monsterhealth, Main.heroname,heroat,monsterat);
            
            
                HeroAtk();
                MonsAtk();
               
                Console.ReadLine();
            }
            while (herohealth > 0 && monsterhealth > 0);
        }
        public void HeroAtk()
        {
            heroat = rand.Next(10, 21);
            monsterhealth=monsterhealth - heroat;
           
        }
        public void MonsAtk()
        {
            monsterat = rand.Next(10, 20);
            herohealth = herohealth - monsterat;
           
        }
        
    }
}

还有其他一些值得做的改变,但他们可以等到你有这个工作。

There are a couple of other changes it'd be worth doing, but they can wait until you have this working.


你好,





我认为你已经混淆了Deep Copy和浅层复制



定义:



Hi,


I think you have mixed up with Deep Copy and Shallow Copy

Definition:

Quote:

术语浅拷贝和深拷贝,指的是复制对象的方式,例如,在调用复制构造函数或赋值运算符期间。深拷贝也可以被称为成员明智的拷贝,并且拷贝操作尊重对象语义。例如,复制具有标准字符串类型成员的对象可确保目标对象中的相应标准字符串由类字符串的复制构造函数进行复制构造。



浅拷贝:这只是创建一个新对象,然后将当前对象的非静态字段复制到新对象。如果字段是值类型,则执行字段的逐位复制。如果它是引用类型,则复制引用但不复制引用对象。因此,原始对象及其克隆引用相同的对象。



深层复制:深层复制与浅层复制部分相同,但区别在于深层复制复制整个对象并生成不同的对象,这意味着它不引用对于原始对象,在浅复制的情况下,目标对象总是引用原始对象,并且目标对象中的更改也会对原始对象进行更改。它序列化对象并反序列化输出。

The terms "Shallow Copy " and "Deep Copy " which refer to the way the objects are copied, for example, during the invocation of a copy constructor or assignment operator. The deep copy can also be called as member wise copy and the copy operation respects object semantics. For example, copying an object that has a member of type standard string ensures that the corresponding standard string in the target object is copy-constructed by the copy constructor of class string.

Shallow copy: This is nothing but creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type then a bit-by-bit copy of the field is performed. If it is a reference type then the reference is copied but not the referred object. Therefore, the original object and its clone refer to the same object.

Deep copy: Deep copy is partially same as shallow copy, but the difference is deep copy copies the whole object and makes a different object, it means it do not refer to the original object while in case of shallow copy the target object always refer to the original object and changes in target object also make changes in original object. It serializes the objects and deserializes the output.





解释清楚:http://seesharpconcepts.blogspot.in/2012/05/shallow-copy-and-deep-copy-in-c.html [ ^ ]



您使用过的Int是结构,因此当您从另一个整数中分配一个整数时,它将深度复制值,这意味着它不会保留参考,所以如果你想要得到相同的,你可以使用 REF [ ^ ]但请注意不要使用 OUT [ ^ ]因为您需要在函数中指定它,但是您需要在函数中继续其值,并且在操作之后您将获得操纵值。



享受..



Well explained : http://seesharpconcepts.blogspot.in/2012/05/shallow-copy-and-deep-copy-in-c.html[^]

You have used Int which is structure so when you assign an integer from another integer it will deep copy the values which mean it will not hold the reference so if you want achive the same you can use REF[^] but mind dont use OUT[^] because you need to assign it with in the function but you need to continue its value within the function and after manipulation you will get the manipulated value.

Enjoy..


这篇关于C#初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆