在不同的方法中使用相同的变量。 [英] Using the same variable in different methods.

查看:246
本文介绍了在不同的方法中使用相同的变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里非常新,所以请保持愉快^^"

从本质上讲,我的代码目前总体上是不完整的,我知道,但我希望能够使用一个变量不同的方法。我的目标是制作一个小型RPG,以帮助我学习如何使用C#

VERY new here, so be nice please ^^"
Essentially, my code is incomplete overall right now, I know, but I want to be able to use one variable in different methods. I aim to make a lil RPG ish thing to help me learn how to use C#

变量设置为1< | [这是我希望能够使用的变量不同方法]

Variable Turn set to 1 <|[This is the variable that I want to be able to use across different methods]

考虑变量的转换语句转¥
转1 - 将其传递给我为转1创建的方法

转2 - 将它传递给我为第2轮创建的方法

等等,所以第四个。



每回合应该单独以Turn + = 1结束,这样每个回合的个人方法可以增加转弯次数。我可以通过主转弯命令来做到这一点,真的,但是后来我没有那么多学习(除此之外我不能搞乱时间旅行
并且很容易弄乱人们的转弯,什么是RPG没有混淆时间旅行?)



当前不完整的代码是:

Switch statement that considers the variable Turn
Turn 1 - Pass it on to the method I am creating for turn 1
Turn 2 - Pass it on to the method I am creating for turn 2
so on and so fourth.

Each turn should separately end with Turn += 1 so that each turn's personal method can increase the turn count. I can probably do it through the main turn command, true, but then I am not learning as much (and besides I can't mess about with time travel and messing with people's turns as easily, and what is an RPG without confusing time travel?)

The current incomplete code is:

using System;
using TurnAction;


namespace lilGame
{
    class Program
    {
        public static void Main()
        {
            if(Turn.turns == 0)
            {

            }

        }
    }
}







其中一个项目和另一个使用System的


for one of the programs and the other

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

namespace TurnAction
{
    //  public class Turn
    public class Turn
    {

        /*      
                private int Turns { get; set; }
                {
                    get
                    {
                        return turns; 
                   }
                }
        */

        public double SetVariableTurns(double turns)
        {
            float turns = 1;
        }

        public void Left1(Turn turns)
        {
            Console.WriteLine("There's a big monster. A really big one. It eats you.");
            turns = 1;
            string Direction = Console.ReadLine();
        }

        public void Right1()
        {
            Console.WriteLine("You find a single, lonely gold coin. A bit like you, but it's actually worth something.");
            string Direction = Console.ReadLine();
        }

        public void Turn1()
        {
            string Direction = Console.ReadLine();
             

            {
                Console.WriteLine("You stand there contemplating how one might spell Left or Right, you are pretty sure it is not spelled {0}.", Direction);
                string Wait = Console.ReadLine();
                ;
            }
        }
    }
    

((有人试图帮助我引导我使用两个单独的程序,一个用于管理转弯计数,一个用于单独转弯,并且让我完成了如何自己完成它的研究,但它已经让我感到困惑所以我可能会去
撤消它并将它全部移回一个程序,然后在开始时使用我原来的开关和单独转弯方法的想法。))

(( Someone who was trying to help me lead me halfway through using two separate programs, one for managing the turn count and one for the separate turns, and led me to finish researching how to finish it myself, but it's already confusing me so I am probably going to undo it and move it all back to one program and just go with my original idea of a switch at the start and the separate turn methods after. ))

嗯,非常感谢您只看这个烂摊子。我打算将其清理干净并将其全部移回一个程序,但正如我所说,即使是公共的,我也不能在多种方法中使用一个变量,有什么帮助吗?

Well, thank you very much for just looking at this mess. I intend to clean it up and move it all back to one program, but as I said I can't use one variable in multiple methods even when it is public, any help?

推荐答案

目前,您的方法中有变量local。你需要的是一个类级变量,以便可以在该类的任何方法中访问它。所以你可以写你的类如:

Currently you have variable local in your method. What you need is a class level variable so that it can be accessible in any method in that class. So you can write your class like:

public class Turn
{
   // class level variable
   public int turns = 0;

   public double SetVariableTurns(double turns)
   {
      // now set value in any method like below
      turns = 1;
   }

现在在你的main方法中你可以使用它:

and now in your main method you can use it :

public static void Main()
{
   Turn objTurn = new Turn();
   if(objTurn.turns == 0)
   {

   }

}

希望它能给你一些想法。

Hope it gives you idea.





这篇关于在不同的方法中使用相同的变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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